readline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* readline.c --- line editing support for Guile */
  2. /* Copyright (C) 1997,1999,2000,2001, 2002, 2003, 2006, 2007, 2008 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. /* Include private, configure generated header (i.e. config.h). */
  21. #include "guile-readline-config.h"
  22. #ifdef HAVE_RL_GETC_FUNCTION
  23. #include "libguile.h"
  24. #include "libguile/gh.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. };
  48. extern void stifle_history (int max);
  49. SCM_DEFINE (scm_readline_options, "readline-options-interface", 0, 1, 0,
  50. (SCM setting),
  51. "")
  52. #define FUNC_NAME s_scm_readline_options
  53. {
  54. SCM ans = scm_options (setting,
  55. scm_readline_opts,
  56. SCM_N_READLINE_OPTIONS,
  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 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_getc (input_port);
  120. }
  121. static int in_readline = 0;
  122. static SCM reentry_barrier_mutex;
  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_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. ans = scm_internal_catch (SCM_BOOL_T,
  169. (scm_t_catch_body) internal_readline,
  170. (void *) SCM_UNPACK (text),
  171. handle_error, 0);
  172. #ifndef __MINGW32__
  173. fclose (rl_instream);
  174. fclose (rl_outstream);
  175. #endif
  176. --in_readline;
  177. return ans;
  178. }
  179. #undef FUNC_NAME
  180. static void
  181. reentry_barrier ()
  182. {
  183. int reentryp = 0;
  184. /* We should rather use scm_try_mutex when it becomes available */
  185. scm_lock_mutex (reentry_barrier_mutex);
  186. if (in_readline)
  187. reentryp = 1;
  188. else
  189. ++in_readline;
  190. scm_unlock_mutex (reentry_barrier_mutex);
  191. if (reentryp)
  192. scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
  193. }
  194. static SCM
  195. handle_error (void *data, SCM tag, SCM args)
  196. {
  197. rl_free_line_state ();
  198. rl_cleanup_after_signal ();
  199. fputc ('\n', rl_outstream); /* We don't want next output on this line */
  200. #ifndef __MINGW32__
  201. fclose (rl_instream);
  202. fclose (rl_outstream);
  203. #endif
  204. --in_readline;
  205. scm_handle_by_throw (data, tag, args);
  206. return SCM_UNSPECIFIED; /* never reached */
  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. ret = scm_from_locale_string (s);
  218. else
  219. ret = SCM_EOF_VAL;
  220. if (!SCM_UNBNDP (text))
  221. free (prompt);
  222. free (s);
  223. return ret;
  224. }
  225. static FILE *
  226. stream_from_fport (SCM port, char *mode, const char *subr)
  227. {
  228. int fd;
  229. FILE *f;
  230. fd = dup (((struct scm_t_fport *) SCM_STREAM (port))->fdes);
  231. if (fd == -1)
  232. {
  233. --in_readline;
  234. scm_syserror (subr);
  235. }
  236. f = fdopen (fd, mode);
  237. if (f == NULL)
  238. {
  239. --in_readline;
  240. scm_syserror (subr);
  241. }
  242. return f;
  243. }
  244. void
  245. scm_readline_init_ports (SCM inp, SCM outp)
  246. {
  247. if (SCM_UNBNDP (inp))
  248. inp = scm_current_input_port ();
  249. if (SCM_UNBNDP (outp))
  250. outp = scm_current_output_port ();
  251. if (!SCM_OPINFPORTP (inp)) {
  252. scm_misc_error (0,
  253. "Input port is not open or not a file port",
  254. SCM_EOL);
  255. }
  256. if (!SCM_OPOUTFPORTP (outp)) {
  257. scm_misc_error (0,
  258. "Output port is not open or not a file port",
  259. SCM_EOL);
  260. }
  261. input_port = inp;
  262. #ifndef __MINGW32__
  263. rl_instream = stream_from_fport (inp, "r", s_scm_readline);
  264. rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
  265. #endif
  266. }
  267. SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
  268. (SCM text),
  269. "")
  270. #define FUNC_NAME s_scm_add_history
  271. {
  272. char* s;
  273. s = scm_to_locale_string (text);
  274. add_history (s);
  275. free (s);
  276. return SCM_UNSPECIFIED;
  277. }
  278. #undef FUNC_NAME
  279. SCM_DEFINE (scm_read_history, "read-history", 1, 0, 0,
  280. (SCM file),
  281. "")
  282. #define FUNC_NAME s_scm_read_history
  283. {
  284. char *filename;
  285. SCM ret;
  286. filename = scm_to_locale_string (file);
  287. ret = scm_from_bool (!read_history (filename));
  288. free (filename);
  289. return ret;
  290. }
  291. #undef FUNC_NAME
  292. SCM_DEFINE (scm_write_history, "write-history", 1, 0, 0,
  293. (SCM file),
  294. "")
  295. #define FUNC_NAME s_scm_write_history
  296. {
  297. char *filename;
  298. SCM ret;
  299. filename = scm_to_locale_string (file);
  300. ret = scm_from_bool (!write_history (filename));
  301. free (filename);
  302. return ret;
  303. }
  304. #undef FUNC_NAME
  305. SCM_DEFINE (scm_clear_history, "clear-history", 0, 0, 0,
  306. (),
  307. "Clear the history buffer of the readline machinery.")
  308. #define FUNC_NAME s_scm_clear_history
  309. {
  310. clear_history();
  311. return SCM_UNSPECIFIED;
  312. }
  313. #undef FUNC_NAME
  314. SCM_DEFINE (scm_filename_completion_function, "filename-completion-function", 2, 0, 0,
  315. (SCM text, SCM continuep),
  316. "")
  317. #define FUNC_NAME s_scm_filename_completion_function
  318. {
  319. char *s;
  320. SCM ans;
  321. char *c_text = scm_to_locale_string (text);
  322. #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
  323. s = rl_filename_completion_function (c_text, scm_is_true (continuep));
  324. #else
  325. s = filename_completion_function (c_text, scm_is_true (continuep));
  326. #endif
  327. ans = scm_take_locale_string (s);
  328. free (c_text);
  329. return ans;
  330. }
  331. #undef FUNC_NAME
  332. /*
  333. * The following has been modified from code contributed by
  334. * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
  335. */
  336. SCM scm_readline_completion_function_var;
  337. static char *
  338. completion_function (char *text, int continuep)
  339. {
  340. SCM compfunc = SCM_VARIABLE_REF (scm_readline_completion_function_var);
  341. SCM res;
  342. if (scm_is_false (compfunc))
  343. return NULL; /* #f => completion disabled */
  344. else
  345. {
  346. SCM t = scm_from_locale_string (text);
  347. SCM c = scm_from_bool (continuep);
  348. res = scm_apply (compfunc, scm_list_2 (t, c), SCM_EOL);
  349. if (scm_is_false (res))
  350. return NULL;
  351. return scm_to_locale_string (res);
  352. }
  353. }
  354. #if HAVE_RL_GET_KEYMAP
  355. /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
  356. static int match_paren (int x, int k);
  357. static int find_matching_paren (int k);
  358. static void init_bouncing_parens ();
  359. static void
  360. init_bouncing_parens ()
  361. {
  362. if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
  363. {
  364. rl_bind_key (')', match_paren);
  365. rl_bind_key (']', match_paren);
  366. rl_bind_key ('}', match_paren);
  367. }
  368. }
  369. static int
  370. find_matching_paren(int k)
  371. {
  372. register int i;
  373. register char c = 0;
  374. int end_parens_found = 0;
  375. /* Choose the corresponding opening bracket. */
  376. if (k == ')') c = '(';
  377. else if (k == ']') c = '[';
  378. else if (k == '}') c = '{';
  379. for (i=rl_point-2; i>=0; i--)
  380. {
  381. /* Is the current character part of a character literal? */
  382. if (i - 2 >= 0
  383. && rl_line_buffer[i - 1] == '\\'
  384. && rl_line_buffer[i - 2] == '#')
  385. ;
  386. else if (rl_line_buffer[i] == k)
  387. end_parens_found++;
  388. else if (rl_line_buffer[i] == '"')
  389. {
  390. /* Skip over a string literal. */
  391. for (i--; i >= 0; i--)
  392. if (rl_line_buffer[i] == '"'
  393. && ! (i - 1 >= 0
  394. && rl_line_buffer[i - 1] == '\\'))
  395. break;
  396. }
  397. else if (rl_line_buffer[i] == c)
  398. {
  399. if (end_parens_found==0)
  400. return i;
  401. else --end_parens_found;
  402. }
  403. }
  404. return -1;
  405. }
  406. static int
  407. match_paren (int x, int k)
  408. {
  409. int tmp;
  410. #ifndef __MINGW32__
  411. int fno;
  412. SELECT_TYPE readset;
  413. struct timeval timeout;
  414. #endif
  415. rl_insert (x, k);
  416. if (!SCM_READLINE_BOUNCE_PARENS)
  417. return 0;
  418. /* Did we just insert a quoted paren? If so, then don't bounce. */
  419. if (rl_point - 1 >= 1
  420. && rl_line_buffer[rl_point - 2] == '\\')
  421. return 0;
  422. #ifndef __MINGW32__
  423. tmp = 1000 * SCM_READLINE_BOUNCE_PARENS;
  424. timeout.tv_sec = tmp / 1000000;
  425. timeout.tv_usec = tmp % 1000000;
  426. FD_ZERO (&readset);
  427. fno = fileno (rl_instream);
  428. FD_SET (fno, &readset);
  429. #endif
  430. if (rl_point > 1)
  431. {
  432. tmp = rl_point;
  433. rl_point = find_matching_paren (k);
  434. if (rl_point > -1)
  435. {
  436. rl_redisplay ();
  437. #ifndef __MINGW32__
  438. scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
  439. #else
  440. WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE),
  441. SCM_READLINE_BOUNCE_PARENS);
  442. #endif
  443. }
  444. rl_point = tmp;
  445. }
  446. return 0;
  447. }
  448. #endif /* HAVE_RL_GET_KEYMAP */
  449. #endif /* HAVE_RL_GETC_FUNCTION */
  450. void
  451. scm_init_readline ()
  452. {
  453. #ifdef HAVE_RL_GETC_FUNCTION
  454. #include "guile-readline/readline.x"
  455. scm_readline_completion_function_var
  456. = scm_c_define ("*readline-completion-function*", SCM_BOOL_F);
  457. #ifndef __MINGW32__
  458. rl_getc_function = current_input_getc;
  459. #endif
  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. reentry_barrier_mutex = scm_permanent_object (scm_make_mutex ());
  468. scm_init_opts (scm_readline_options,
  469. scm_readline_opts,
  470. SCM_N_READLINE_OPTIONS);
  471. #if HAVE_RL_GET_KEYMAP
  472. init_bouncing_parens();
  473. #endif
  474. scm_add_feature ("readline");
  475. #endif /* HAVE_RL_GETC_FUNCTION */
  476. }
  477. /*
  478. Local Variables:
  479. c-file-style: "gnu"
  480. End:
  481. */