readline.c 12 KB

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