readline.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* readline.c --- line editing support for Guile */
  2. /* Copyright (C) 1997,1999,2000,2001, 2003 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 2, 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. #if HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include "libguile/_scm.h"
  24. #ifdef HAVE_RL_GETC_FUNCTION
  25. #include "libguile.h"
  26. #include "libguile/gh.h"
  27. #include "libguile/iselect.h"
  28. #include <stdio.h>
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <readline/readline.h>
  33. #include <readline/history.h>
  34. #include <sys/time.h>
  35. #include <signal.h>
  36. #include "libguile/validate.h"
  37. #include "guile-readline/readline.h"
  38. scm_t_option scm_readline_opts[] = {
  39. { SCM_OPTION_BOOLEAN, "history-file", 1,
  40. "Use history file." },
  41. { SCM_OPTION_INTEGER, "history-length", 200,
  42. "History length." },
  43. { SCM_OPTION_INTEGER, "bounce-parens", 500,
  44. "Time (ms) to show matching opening parenthesis (0 = off)."}
  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. SCM_N_READLINE_OPTIONS,
  55. FUNC_NAME);
  56. stifle_history (SCM_HISTORY_LENGTH);
  57. return ans;
  58. }
  59. #undef FUNC_NAME
  60. #ifndef HAVE_STRDUP
  61. static char *
  62. strdup (char *s)
  63. {
  64. size_t len = strlen (s);
  65. char *new = malloc (len + 1);
  66. strcpy (new, s);
  67. return new;
  68. }
  69. #endif /* HAVE_STRDUP */
  70. #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
  71. /* These are readline functions added in release 2.3. They will work
  72. * together with readline-2.1 and 2.2. (The readline interface is
  73. * disabled for earlier releases.)
  74. * They are declared static; if we want to use them elsewhere, then
  75. * we need external declarations for them, but at the moment, I don't
  76. * think anything else in Guile ought to use these.
  77. */
  78. extern void _rl_clean_up_for_exit ();
  79. extern void _rl_kill_kbd_macro ();
  80. extern int _rl_init_argument ();
  81. void
  82. rl_cleanup_after_signal ()
  83. {
  84. #ifdef HAVE_RL_CLEAR_SIGNALS
  85. _rl_clean_up_for_exit ();
  86. #endif
  87. (*rl_deprep_term_function) ();
  88. #ifdef HAVE_RL_CLEAR_SIGNALS
  89. rl_clear_signals ();
  90. #endif
  91. rl_pending_input = 0;
  92. }
  93. void
  94. rl_free_line_state ()
  95. {
  96. register HIST_ENTRY *entry;
  97. free_undo_list ();
  98. entry = current_history ();
  99. if (entry)
  100. entry->data = (char *)NULL;
  101. _rl_kill_kbd_macro ();
  102. rl_clear_message ();
  103. _rl_init_argument ();
  104. }
  105. #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
  106. static int promptp;
  107. static SCM input_port;
  108. static SCM before_read;
  109. static int
  110. current_input_getc (FILE *in SCM_UNUSED)
  111. {
  112. if (promptp && !SCM_FALSEP (before_read))
  113. {
  114. scm_apply (before_read, SCM_EOL, SCM_EOL);
  115. promptp = 0;
  116. }
  117. return scm_getc (input_port);
  118. }
  119. static int in_readline = 0;
  120. #ifdef USE_THREADS
  121. static scm_t_mutex reentry_barrier_mutex;
  122. #endif
  123. static SCM internal_readline (SCM text);
  124. static SCM handle_error (void *data, SCM tag, SCM args);
  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_STRINGP (text))
  137. {
  138. --in_readline;
  139. scm_wrong_type_arg (s_scm_readline, SCM_ARG1, text);
  140. }
  141. SCM_STRING_COERCE_0TERMINATION_X (text);
  142. }
  143. if (!((SCM_UNBNDP (inp) && SCM_OPINFPORTP (scm_cur_inp))
  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_cur_outp))
  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_FALSEP (read_hook)))
  160. {
  161. if (!(SCM_NFALSEP (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. ans = scm_internal_catch (SCM_BOOL_T,
  170. (scm_t_catch_body) internal_readline,
  171. (void *) SCM_UNPACK (text),
  172. handle_error, 0);
  173. fclose (rl_instream);
  174. fclose (rl_outstream);
  175. --in_readline;
  176. return ans;
  177. }
  178. #undef FUNC_NAME
  179. static void
  180. reentry_barrier ()
  181. {
  182. int reentryp = 0;
  183. #ifdef USE_THREADS
  184. /* We should rather use scm_t_mutexry_lock when it becomes available */
  185. scm_mutex_lock (&reentry_barrier_mutex);
  186. #endif
  187. if (in_readline)
  188. reentryp = 1;
  189. else
  190. ++in_readline;
  191. #ifdef USE_THREADS
  192. scm_mutex_unlock (&reentry_barrier_mutex);
  193. #endif
  194. if (reentryp)
  195. scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
  196. }
  197. static SCM
  198. handle_error (void *data, SCM tag, SCM args)
  199. {
  200. rl_free_line_state ();
  201. rl_cleanup_after_signal ();
  202. fputc ('\n', rl_outstream); /* We don't want next output on this line */
  203. fclose (rl_instream);
  204. fclose (rl_outstream);
  205. --in_readline;
  206. scm_handle_by_throw (data, tag, args);
  207. return SCM_UNSPECIFIED; /* never reached */
  208. }
  209. static SCM
  210. internal_readline (SCM text)
  211. {
  212. SCM ret;
  213. char *s;
  214. char *prompt = SCM_UNBNDP (text) ? "" : SCM_STRING_CHARS (text);
  215. promptp = 1;
  216. s = readline (prompt);
  217. if (s)
  218. ret = scm_makfrom0str (s);
  219. else
  220. ret = SCM_EOF_VAL;
  221. free (s);
  222. return ret;
  223. }
  224. static FILE *
  225. stream_from_fport (SCM port, char *mode, const char *subr)
  226. {
  227. int fd;
  228. FILE *f;
  229. fd = dup (((struct scm_t_fport *) SCM_STREAM (port))->fdes);
  230. if (fd == -1)
  231. {
  232. --in_readline;
  233. scm_syserror (subr);
  234. }
  235. f = fdopen (fd, mode);
  236. if (f == NULL)
  237. {
  238. --in_readline;
  239. scm_syserror (subr);
  240. }
  241. return f;
  242. }
  243. void
  244. scm_readline_init_ports (SCM inp, SCM outp)
  245. {
  246. if (SCM_UNBNDP (inp))
  247. inp = scm_cur_inp;
  248. if (SCM_UNBNDP (outp))
  249. outp = scm_cur_outp;
  250. if (!SCM_OPINFPORTP (inp)) {
  251. scm_misc_error (0,
  252. "Input port is not open or not a file port",
  253. SCM_EOL);
  254. }
  255. if (!SCM_OPOUTFPORTP (outp)) {
  256. scm_misc_error (0,
  257. "Output port is not open or not a file port",
  258. SCM_EOL);
  259. }
  260. input_port = inp;
  261. rl_instream = stream_from_fport (inp, "r", s_scm_readline);
  262. rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
  263. }
  264. SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
  265. (SCM text),
  266. "")
  267. #define FUNC_NAME s_scm_add_history
  268. {
  269. char* s;
  270. SCM_VALIDATE_STRING (1,text);
  271. SCM_STRING_COERCE_0TERMINATION_X (text);
  272. s = SCM_STRING_CHARS (text);
  273. add_history (strdup (s));
  274. return SCM_UNSPECIFIED;
  275. }
  276. #undef FUNC_NAME
  277. SCM_DEFINE (scm_read_history, "read-history", 1, 0, 0,
  278. (SCM file),
  279. "")
  280. #define FUNC_NAME s_scm_read_history
  281. {
  282. SCM_VALIDATE_STRING (1,file);
  283. return SCM_NEGATE_BOOL (read_history (SCM_STRING_CHARS (file)));
  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. SCM_VALIDATE_STRING (1,file);
  292. return SCM_NEGATE_BOOL (write_history (SCM_STRING_CHARS (file)));
  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. SCM_VALIDATE_STRING (1,text);
  312. SCM_STRING_COERCE_0TERMINATION_X (text);
  313. #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
  314. s = rl_filename_completion_function (SCM_STRING_CHARS (text), SCM_NFALSEP (continuep));
  315. #else
  316. s = filename_completion_function (SCM_STRING_CHARS (text), SCM_NFALSEP (continuep));
  317. #endif
  318. ans = scm_makfrom0str (s);
  319. free (s);
  320. return ans;
  321. }
  322. #undef FUNC_NAME
  323. /*
  324. * The following has been modified from code contributed by
  325. * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
  326. */
  327. SCM scm_readline_completion_function_var;
  328. static char *
  329. completion_function (char *text, int continuep)
  330. {
  331. SCM compfunc = SCM_VARIABLE_REF (scm_readline_completion_function_var);
  332. SCM res;
  333. if (SCM_FALSEP (compfunc))
  334. return NULL; /* #f => completion disabled */
  335. else
  336. {
  337. SCM t = scm_makfrom0str (text);
  338. SCM c = continuep ? SCM_BOOL_T : SCM_BOOL_F;
  339. res = scm_apply (compfunc, scm_list_2 (t, c), SCM_EOL);
  340. if (SCM_FALSEP (res))
  341. return NULL;
  342. if (!SCM_STRINGP (res))
  343. scm_misc_error (s_scm_readline,
  344. "Completion function returned bogus value: %S",
  345. scm_list_1 (res));
  346. SCM_STRING_COERCE_0TERMINATION_X (res);
  347. return strdup (SCM_STRING_CHARS (res));
  348. }
  349. }
  350. /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
  351. static int match_paren (int x, int k);
  352. static int find_matching_paren (int k);
  353. static void init_bouncing_parens ();
  354. static void
  355. init_bouncing_parens ()
  356. {
  357. if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
  358. {
  359. rl_bind_key (')', match_paren);
  360. rl_bind_key (']', match_paren);
  361. rl_bind_key ('}', match_paren);
  362. }
  363. }
  364. static int
  365. find_matching_paren(int k)
  366. {
  367. register int i;
  368. register char c = 0;
  369. int end_parens_found = 0;
  370. /* Choose the corresponding opening bracket. */
  371. if (k == ')') c = '(';
  372. else if (k == ']') c = '[';
  373. else if (k == '}') c = '{';
  374. for (i=rl_point-2; i>=0; i--)
  375. {
  376. /* Is the current character part of a character literal? */
  377. if (i - 2 >= 0
  378. && rl_line_buffer[i - 1] == '\\'
  379. && rl_line_buffer[i - 2] == '#')
  380. ;
  381. else if (rl_line_buffer[i] == k)
  382. end_parens_found++;
  383. else if (rl_line_buffer[i] == '"')
  384. {
  385. /* Skip over a string literal. */
  386. for (i--; i >= 0; i--)
  387. if (rl_line_buffer[i] == '"'
  388. && ! (i - 1 >= 0
  389. && rl_line_buffer[i - 1] == '\\'))
  390. break;
  391. }
  392. else if (rl_line_buffer[i] == c)
  393. {
  394. if (end_parens_found==0)
  395. return i;
  396. else --end_parens_found;
  397. }
  398. }
  399. return -1;
  400. }
  401. static int
  402. match_paren (int x, int k)
  403. {
  404. int tmp, fno;
  405. SELECT_TYPE 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. scm_internal_select (fno + 1, &readset, NULL, NULL, &timeout);
  428. }
  429. rl_point = tmp;
  430. }
  431. return 0;
  432. }
  433. #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
  434. /* Readline disables SA_RESTART on SIGWINCH.
  435. * This code turns it back on.
  436. */
  437. static int
  438. sigwinch_enable_restart (void)
  439. {
  440. #ifdef HAVE_SIGINTERRUPT
  441. siginterrupt (SIGWINCH, 0);
  442. #else
  443. struct sigaction action;
  444. sigaction (SIGWINCH, NULL, &action);
  445. action.sa_flags |= SA_RESTART;
  446. sigaction (SIGWINCH, &action, NULL);
  447. #endif
  448. return 0;
  449. }
  450. #endif
  451. #endif /* HAVE_RL_GETC_FUNCTION */
  452. void
  453. scm_init_readline ()
  454. {
  455. #ifdef HAVE_RL_GETC_FUNCTION
  456. #include "guile-readline/readline.x"
  457. scm_readline_completion_function_var
  458. = scm_c_define ("*readline-completion-function*", SCM_BOOL_F);
  459. rl_getc_function = current_input_getc;
  460. #if defined (_RL_FUNCTION_TYPEDEF)
  461. rl_completion_entry_function = (rl_compentry_func_t*) completion_function;
  462. #else
  463. rl_completion_entry_function = (Function*) completion_function;
  464. #endif
  465. rl_basic_word_break_characters = "\t\n\"'`;()";
  466. rl_readline_name = "Guile";
  467. #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
  468. rl_pre_input_hook = sigwinch_enable_restart;
  469. #endif
  470. #ifdef USE_THREADS
  471. scm_mutex_init (&reentry_barrier_mutex);
  472. #endif
  473. scm_init_opts (scm_readline_options,
  474. scm_readline_opts,
  475. SCM_N_READLINE_OPTIONS);
  476. init_bouncing_parens();
  477. scm_add_feature ("readline");
  478. #endif /* HAVE_RL_GETC_FUNCTION */
  479. }
  480. /*
  481. Local Variables:
  482. c-file-style: "gnu"
  483. End:
  484. */