Ruby  1.9.3p551(2014-11-13revision48407)
readline.c
Go to the documentation of this file.
1 /************************************************
2 
3  readline.c - GNU Readline module
4 
5  $Author: usa $
6  created at: Wed Jan 20 13:59:32 JST 1999
7 
8  Copyright (C) 1997-2008 Shugo Maeda
9  Copyright (C) 2008-2009 TAKAO Kouji
10 
11  $Id: readline.c 39377 2013-02-22 05:27:47Z usa $
12 
13  Contact:
14  - TAKAO Kouji <kouji at takao7 dot net> (current maintainer)
15 
16 ************************************************/
17 
18 #ifdef RUBY_EXTCONF_H
19 #include RUBY_EXTCONF_H
20 #endif
21 
22 #include "ruby/config.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_READLINE_READLINE_H
27 #include <readline/readline.h>
28 #endif
29 #ifdef HAVE_READLINE_HISTORY_H
30 #include <readline/history.h>
31 #endif
32 #ifdef HAVE_EDITLINE_READLINE_H
33 #include <editline/readline.h>
34 #endif
35 
36 #include "ruby/ruby.h"
37 #include "ruby/io.h"
38 
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 
43 #ifdef HAVE_SYS_STAT_H
44 #include <sys/stat.h>
45 #endif
46 
48 
49 #define EDIT_LINE_LIBRARY_VERSION "EditLine wrapper"
50 #ifndef USE_INSERT_IGNORE_ESCAPE
51 # if !defined(HAVE_EDITLINE_READLINE_H) && defined(HAVE_RL_PROMPT_START_IGNORE) && defined(HAVE_RL_PROMPT_END_IGNORE)
52 # define USE_INSERT_IGNORE_ESCAPE 1
53 # else
54 # define USE_INSERT_IGNORE_ESCAPE 0
55 # endif
56 #endif
57 
58 #define COMPLETION_PROC "completion_proc"
59 #define COMPLETION_CASE_FOLD "completion_case_fold"
61 #if USE_INSERT_IGNORE_ESCAPE
62 static ID id_orig_prompt, id_last_prompt;
63 #endif
64 
65 #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
66 # define rl_filename_completion_function filename_completion_function
67 #endif
68 #ifndef HAVE_RL_USERNAME_COMPLETION_FUNCTION
69 # define rl_username_completion_function username_completion_function
70 #endif
71 #ifndef HAVE_RL_COMPLETION_MATCHES
72 # define rl_completion_matches completion_matches
73 #endif
74 
77 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
78 static int readline_completion_append_character;
79 #endif
80 
81 static char **readline_attempted_completion_function(const char *text,
82  int start, int end);
83 
84 #define OutputStringValue(str) do {\
85  SafeStringValue(str);\
86  (str) = rb_str_conv_enc((str), rb_enc_get(str), rb_locale_encoding());\
87 } while (0)\
88 
89 
90 /*
91  * Document-class: Readline
92  *
93  * The Readline module provides interface for GNU Readline.
94  * This module defines a number of methods to facilitate completion
95  * and accesses input history from the Ruby interpreter.
96  * This module supported Edit Line(libedit) too.
97  * libedit is compatible with GNU Readline.
98  *
99  * GNU Readline:: http://www.gnu.org/directory/readline.html
100  * libedit:: http://www.thrysoee.dk/editline/
101  *
102  * Reads one inputted line with line edit by Readline.readline method.
103  * At this time, the facilitatation completion and the key
104  * bind like Emacs can be operated like GNU Readline.
105  *
106  * require "readline"
107  * while buf = Readline.readline("> ", true)
108  * p buf
109  * end
110  *
111  * The content that the user input can be recorded to the history.
112  * The history can be accessed by Readline::HISTORY constant.
113  *
114  * require "readline"
115  * while buf = Readline.readline("> ", true)
116  * p Readline::HISTORY.to_a
117  * print("-> ", buf, "\n")
118  * end
119  *
120  * Most of methods raise SecurityError exception if $SAFE is 4.
121  *
122  * Documented by TAKAO Kouji <kouji at takao7 dot net>.
123  */
124 
125 #if defined HAVE_RL_GETC_FUNCTION
126 static VALUE readline_instream;
127 static ID id_getbyte;
128 
129 #ifndef HAVE_RL_GETC
130 #define rl_getc(f) EOF
131 #endif
132 
133 static int readline_getc(FILE *);
134 static int
135 readline_getc(FILE *input)
136 {
137  rb_io_t *ifp = 0;
138  VALUE c;
139  if (!readline_instream) return rl_getc(input);
140  GetOpenFile(readline_instream, ifp);
141  if (rl_instream != ifp->stdio_file) return rl_getc(input);
142 #if defined(_WIN32)
143  {
144  INPUT_RECORD ir;
145  int n;
146  static int prior_key = '0';
147  for (;;) {
148  if (prior_key > 0xff) {
149  prior_key = rl_getc(ifp->stdio_file);
150  return prior_key;
151  }
152  if (PeekConsoleInput((HANDLE)_get_osfhandle(ifp->fd), &ir, 1, &n)) {
153  if (n == 1) {
154  if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown) {
155  prior_key = rl_getc(ifp->stdio_file);
156  return prior_key;
157  } else {
158  ReadConsoleInput((HANDLE)_get_osfhandle(ifp->fd), &ir, 1, &n);
159  }
160  } else {
161  HANDLE h = (HANDLE)_get_osfhandle(ifp->fd);
162  rb_w32_wait_events(&h, 1, INFINITE);
163  }
164  } else {
165  break;
166  }
167  }
168  }
169 #endif
170  c = rb_funcall(readline_instream, id_getbyte, 0, 0);
171  if (NIL_P(c)) return EOF;
172  return NUM2CHR(c);
173 }
174 #elif defined HAVE_RL_EVENT_HOOK
175 #define BUSY_WAIT 0
176 
177 static int readline_event(void);
178 static int
179 readline_event(void)
180 {
181 #if BUSY_WAIT
183 #else
185  return 0;
186 #endif
187 }
188 #endif
189 
190 #if USE_INSERT_IGNORE_ESCAPE
191 static VALUE
192 insert_ignore_escape(VALUE self, VALUE prompt)
193 {
194  VALUE last_prompt, orig_prompt = rb_attr_get(self, id_orig_prompt);
195  int ignoring = 0;
196  const char *s0, *s, *e;
197  long len;
198  static const char ignore_code[2] = {RL_PROMPT_START_IGNORE, RL_PROMPT_END_IGNORE};
199 
200  prompt = rb_str_new_shared(prompt);
201  last_prompt = rb_attr_get(self, id_last_prompt);
202  if (orig_prompt == prompt) return last_prompt;
203  len = RSTRING_LEN(prompt);
204  if (NIL_P(last_prompt)) {
205  last_prompt = rb_str_tmp_new(len);
206  }
207 
208  s = s0 = RSTRING_PTR(prompt);
209  e = s0 + len;
210  rb_str_set_len(last_prompt, 0);
211  while (s < e && *s) {
212  switch (*s) {
213  case RL_PROMPT_START_IGNORE:
214  ignoring = -1;
215  rb_str_cat(last_prompt, s0, ++s - s0);
216  s0 = s;
217  break;
218  case RL_PROMPT_END_IGNORE:
219  ignoring = 0;
220  rb_str_cat(last_prompt, s0, ++s - s0);
221  s0 = s;
222  break;
223  case '\033':
224  if (++s < e && *s == '[') {
225  rb_str_cat(last_prompt, s0, s - s0 - 1);
226  s0 = s - 1;
227  while (++s < e && *s) {
228  if (ISALPHA(*s)) {
229  if (!ignoring) {
230  ignoring = 1;
231  rb_str_cat(last_prompt, ignore_code+0, 1);
232  }
233  rb_str_cat(last_prompt, s0, ++s - s0);
234  s0 = s;
235  break;
236  }
237  else if (!('0' <= *s && *s <= '9' || *s == ';')) {
238  break;
239  }
240  }
241  }
242  break;
243  default:
244  if (ignoring > 0) {
245  ignoring = 0;
246  rb_str_cat(last_prompt, ignore_code+1, 1);
247  }
248  s++;
249  break;
250  }
251  }
252  if (ignoring > 0) {
253  ignoring = 0;
254  rb_str_cat(last_prompt, ignore_code+1, 1);
255  }
256  rb_str_cat(last_prompt, s0, s - s0);
257 
258  rb_ivar_set(self, id_orig_prompt, prompt);
259  rb_ivar_set(self, id_last_prompt, last_prompt);
260 
261  return last_prompt;
262 }
263 #endif
264 
265 static VALUE
267 {
268 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
269  readline_completion_append_character = rl_completion_append_character;
270 #endif
271  return (VALUE)readline((char *)prompt);
272 }
273 
274 /*
275  * call-seq:
276  * Readline.readline(prompt = "", add_hist = false) -> string or nil
277  *
278  * Shows the +prompt+ and reads the inputted line with line editing.
279  * The inputted line is added to the history if +add_hist+ is true.
280  *
281  * Returns nil when the inputted line is empty and user inputs EOF
282  * (Presses ^D on UNIX).
283  *
284  * Raises IOError exception if below conditions are satisfied.
285  * 1. stdin is not tty.
286  * 2. stdin was closed. (errno is EBADF after called isatty(2).)
287  *
288  * This method supports thread. Switchs the thread context when waits
289  * inputting line.
290  *
291  * Supports line edit when inputs line. Provides VI and Emacs editing mode.
292  * Default is Emacs editing mode.
293  *
294  * NOTE: Terminates ruby interpreter and does not return the terminal
295  * status after user pressed '^C' when wait inputting line.
296  * Give 3 examples that avoid it.
297  *
298  * * Catches the Interrupt exception by pressed ^C after returns
299  * terminal status:
300  *
301  * require "readline"
302  *
303  * stty_save = `stty -g`.chomp
304  * begin
305  * while buf = Readline.readline
306  * p buf
307  * end
308  * rescue Interrupt
309  * system("stty", stty_save)
310  * exit
311  * end
312  * end
313  * end
314  *
315  * * Catches the INT signal by pressed ^C after returns terminal
316  * status:
317  *
318  * require "readline"
319  *
320  * stty_save = `stty -g`.chomp
321  * trap("INT") { system "stty", stty_save; exit }
322  *
323  * while buf = Readline.readline
324  * p buf
325  * end
326  *
327  * * Ignores pressing ^C:
328  *
329  * require "readline"
330  *
331  * trap("INT", "SIG_IGN")
332  *
333  * while buf = Readline.readline
334  * p buf
335  * end
336  *
337  * Can make as follows with Readline::HISTORY constant.
338  * It does not record to the history if the inputted line is empty or
339  * the same it as last one.
340  *
341  * require "readline"
342  *
343  * while buf = Readline.readline("> ", true)
344  * # p Readline::HISTORY.to_a
345  * Readline::HISTORY.pop if /^\s*$/ =~ buf
346  *
347  * begin
348  * if Readline::HISTORY[Readline::HISTORY.length-2] == buf
349  * Readline::HISTORY.pop
350  * end
351  * rescue IndexError
352  * end
353  *
354  * # p Readline::HISTORY.to_a
355  * print "-> ", buf, "\n"
356  * end
357  *
358  * Raises SecurityError exception if $SAFE is 4.
359  */
360 static VALUE
362 {
363  VALUE tmp, add_hist, result;
364  char *prompt = NULL;
365  char *buff;
366  int status;
367 
368  rb_secure(4);
369  if (rb_scan_args(argc, argv, "02", &tmp, &add_hist) > 0) {
370  OutputStringValue(tmp);
371 #if USE_INSERT_IGNORE_ESCAPE
372  tmp = insert_ignore_escape(self, tmp);
373  rb_str_locktmp(tmp);
374 #endif
375  prompt = RSTRING_PTR(tmp);
376  }
377 
378  if (!isatty(fileno(rl_instream)) && errno == EBADF) rb_raise(rb_eIOError, "closed stdin");
379  if (rl_outstream) {
380  struct stat stbuf;
381  int fd = fileno(rl_outstream);
382  if (fd < 0 || fstat(fd, &stbuf) != 0) {
383  rb_raise(rb_eIOError, "closed stdout");
384  }
385  }
386 
387 #ifdef _WIN32
388  rl_prep_terminal(1);
389 #endif
390  buff = (char*)rb_protect(readline_get, (VALUE)prompt, &status);
391 #if USE_INSERT_IGNORE_ESCAPE
392  if (prompt) {
393  rb_str_unlocktmp(tmp);
394  }
395 #endif
396  if (status) {
397 #if defined HAVE_RL_CLEANUP_AFTER_SIGNAL
398  /* restore terminal mode and signal handler*/
399 #if defined HAVE_RL_FREE_LINE_STATE
400  rl_free_line_state();
401 #endif
402  rl_cleanup_after_signal();
403 #elif defined HAVE_RL_DEPREP_TERM_FUNCTION
404  /* restore terminal mode */
405  if (rl_deprep_term_function != NULL) /* NULL in libedit. [ruby-dev:29116] */
406  (*rl_deprep_term_function)();
407  else
408 #else
409  rl_deprep_terminal();
410 #endif
411  rb_jump_tag(status);
412  }
413 
414  if (RTEST(add_hist) && buff) {
415  add_history(buff);
416  }
417  if (buff) {
418  result = rb_locale_str_new_cstr(buff);
419  }
420  else
421  result = Qnil;
422  if (buff) free(buff);
423  return result;
424 }
425 
426 /*
427  * call-seq:
428  * Readline.input = input
429  *
430  * Specifies a File object +input+ that is input stream for
431  * Readline.readline method.
432  *
433  * Raises SecurityError exception if $SAFE is 4.
434  */
435 static VALUE
437 {
438  rb_io_t *ifp;
439 
440  rb_secure(4);
441  Check_Type(input, T_FILE);
442  GetOpenFile(input, ifp);
443  rl_instream = rb_io_stdio_file(ifp);
444 #ifdef HAVE_RL_GETC_FUNCTION
445  readline_instream = input;
446 #endif
447  return input;
448 }
449 
450 /*
451  * call-seq:
452  * Readline.output = output
453  *
454  * Specifies a File object +output+ that is output stream for
455  * Readline.readline method.
456  *
457  * Raises SecurityError exception if $SAFE is 4.
458  */
459 static VALUE
461 {
462  rb_io_t *ofp;
463 
464  rb_secure(4);
465  Check_Type(output, T_FILE);
466  GetOpenFile(output, ofp);
467  rl_outstream = rb_io_stdio_file(ofp);
468  return output;
469 }
470 
471 /*
472  * call-seq:
473  * Readline.completion_proc = proc
474  *
475  * Specifies a Proc object +proc+ to determine completion behavior. It
476  * should take input string and return an array of completion candidates.
477  *
478  * The default completion is used if +proc+ is nil.
479  *
480  * The String that is passed to the Proc depends on the
481  * Readline.completer_word_break_characters property. By default the word
482  * under the cursor is passed to the Proc. For example, if the input is "foo
483  * bar" then only "bar" would be passed to the completion Proc.
484  *
485  * Upon successful completion the Readline.completion_append_character will be
486  * appended to the input so the user can start working on their next argument.
487  *
488  * = Examples
489  *
490  * == Completion for a Static List
491  *
492  * require 'readline'
493  *
494  * LIST = [
495  * 'search', 'download', 'open',
496  * 'help', 'history', 'quit',
497  * 'url', 'next', 'clear',
498  * 'prev', 'past'
499  * ].sort
500  *
501  * comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) }
502  *
503  * Readline.completion_append_character = " "
504  * Readline.completion_proc = comp
505  *
506  * while line = Readline.readline('> ', true)
507  * p line
508  * end
509  *
510  * == Completion For Directory Contents
511  *
512  * require 'readline'
513  *
514  * Readline.completion_append_character = " "
515  * Readline.completion_proc = Proc.new do |str|
516  * Dir[str+'*'].grep(/^#{Regexp.escape(str)}/)
517  * end
518  *
519  * while line = Readline.readline('> ', true)
520  * p line
521  * end
522  *
523  * = Autocomplete strategies
524  *
525  * When working with auto-complete there are some strategies that work well.
526  * To get some ideas you can take a look at the
527  * completion.rb[http://svn.ruby-lang.org/repos/ruby/trunk/lib/irb/completion.rb]
528  * file for irb.
529  *
530  * The common strategy is to take a list of possible completions and filter it
531  * down to those completions that start with the user input. In the above
532  * examples Enumerator.grep is used. The input is escaped to prevent Regexp
533  * special characters from interfering with the matching.
534  *
535  * It may also be helpful to use the Abbrev library to generate completions.
536  *
537  * Raises ArgumentError if +proc+ does not respond to the call method.
538  *
539  * Raises SecurityError if $SAFE is 4.
540  */
541 static VALUE
543 {
544  rb_secure(4);
545  if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call")))
546  rb_raise(rb_eArgError, "argument must respond to `call'");
547  return rb_ivar_set(mReadline, completion_proc, proc);
548 }
549 
550 /*
551  * call-seq:
552  * Readline.completion_proc -> proc
553  *
554  * Returns the completion Proc object.
555  *
556  * Raises SecurityError exception if $SAFE is 4.
557  */
558 static VALUE
560 {
561  rb_secure(4);
563 }
564 
565 /*
566  * call-seq:
567  * Readline.completion_case_fold = bool
568  *
569  * Sets whether or not to ignore case on completion.
570  *
571  * Raises SecurityError exception if $SAFE is 4.
572  */
573 static VALUE
575 {
576  rb_secure(4);
578 }
579 
580 /*
581  * call-seq:
582  * Readline.completion_case_fold -> bool
583  *
584  * Returns true if completion ignores case. If no, returns false.
585  *
586  * NOTE: Returns the same object that is specified by
587  * Readline.completion_case_fold= method.
588  *
589  * require "readline"
590  *
591  * Readline.completion_case_fold = "This is a String."
592  * p Readline.completion_case_fold # => "This is a String."
593  *
594  * Raises SecurityError exception if $SAFE is 4.
595  */
596 static VALUE
598 {
599  rb_secure(4);
601 }
602 
603 #ifdef HAVE_RL_LINE_BUFFER
604 /*
605  * call-seq:
606  * Readline.line_buffer -> string
607  *
608  * Returns the full line that is being edited. This is useful from
609  * within the complete_proc for determining the context of the
610  * completion request.
611  *
612  * The length of +Readline.line_buffer+ and GNU Readline's rl_end are
613  * same.
614  */
615 static VALUE
617 {
618  rb_secure(4);
619  if (rl_line_buffer == NULL)
620  return Qnil;
621  return rb_locale_str_new_cstr(rl_line_buffer);
622 }
623 #else
624 #define readline_s_get_line_buffer rb_f_notimplement
625 #endif
626 
627 #ifdef HAVE_RL_POINT
628 /*
629  * call-seq:
630  * Readline.point -> int
631  *
632  * Returns the index of the current cursor position in
633  * +Readline.line_buffer+.
634  *
635  * The index in +Readline.line_buffer+ which matches the start of
636  * input-string passed to completion_proc is computed by subtracting
637  * the length of input-string from +Readline.point+.
638  *
639  * start = (the length of input-string) - Readline.point
640  */
641 static VALUE
643 {
644  rb_secure(4);
645  return INT2NUM(rl_point);
646 }
647 #else
648 #define readline_s_get_point rb_f_notimplement
649 #endif
650 
651 static char **
653 {
654  VALUE proc, ary, temp;
655  char **result;
656  int case_fold;
657  long i, matches;
658  rb_encoding *enc;
659  VALUE encobj;
660 
662  if (NIL_P(proc))
663  return NULL;
664 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
665  rl_completion_append_character = readline_completion_append_character;
666 #endif
667 #ifdef HAVE_RL_ATTEMPTED_COMPLETION_OVER
668  rl_attempted_completion_over = 1;
669 #endif
671  ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new_cstr(text));
672  if (!RB_TYPE_P(ary, T_ARRAY))
673  ary = rb_Array(ary);
674  matches = RARRAY_LEN(ary);
675  if (matches == 0) return NULL;
676  result = (char**)malloc((matches + 2)*sizeof(char*));
677  if (result == NULL) rb_raise(rb_eNoMemError, "failed to allocate memory");
678  enc = rb_locale_encoding();
679  encobj = rb_enc_from_encoding(enc);
680  for (i = 0; i < matches; i++) {
681  temp = rb_obj_as_string(RARRAY_PTR(ary)[i]);
682  StringValueCStr(temp); /* must be NUL-terminated */
683  rb_enc_check(encobj, temp);
684  result[i + 1] = (char*)malloc(RSTRING_LEN(temp) + 1);
685  if (result[i + 1] == NULL) rb_memerror();
686  strcpy(result[i + 1], RSTRING_PTR(temp));
687  }
688  result[matches + 1] = NULL;
689 
690  if (matches == 1) {
691  result[0] = strdup(result[1]);
692  }
693  else {
694  const char *result1 = result[1];
695  long low = strlen(result1);
696 
697  for (i = 1; i < matches; ++i) {
698  register int c1, c2;
699  long i1, i2, l2;
700  int n1, n2;
701  const char *p2 = result[i + 1];
702 
703  l2 = strlen(p2);
704  for (i1 = i2 = 0; i1 < low && i2 < l2; i1 += n1, i2 += n2) {
705  c1 = rb_enc_codepoint_len(result1 + i1, result1 + low, &n1, enc);
706  c2 = rb_enc_codepoint_len(p2 + i2, p2 + l2, &n2, enc);
707  if (case_fold) {
708  c1 = rb_tolower(c1);
709  c2 = rb_tolower(c2);
710  }
711  if (c1 != c2) break;
712  }
713 
714  low = i1;
715  }
716  result[0] = ALLOC_N(char, low + 1);
717  strncpy(result[0], result[1], low);
718  result[0][low] = '\0';
719  }
720 
721  return result;
722 }
723 
724 #ifdef HAVE_RL_SET_SCREEN_SIZE
725 /*
726  * call-seq:
727  * Readline.set_screen_size(rows, columns) -> self
728  *
729  * Set terminal size to +rows+ and +columns+.
730  *
731  * See GNU Readline's rl_set_screen_size function.
732  *
733  * Raises NotImplementedError if the using readline library does not support.
734  *
735  * Raises SecurityError exception if $SAFE is 4.
736  */
737 static VALUE
738 readline_s_set_screen_size(VALUE self, VALUE rows, VALUE columns)
739 {
740  rb_secure(4);
741  rl_set_screen_size(NUM2INT(rows), NUM2INT(columns));
742  return self;
743 }
744 #else
745 #define readline_s_set_screen_size rb_f_notimplement
746 #endif
747 
748 #ifdef HAVE_RL_GET_SCREEN_SIZE
749 /*
750  * call-seq:
751  * Readline.get_screen_size -> [rows, columns]
752  *
753  * Returns the terminal's rows and columns.
754  *
755  * See GNU Readline's rl_get_screen_size function.
756  *
757  * Raises NotImplementedError if the using readline library does not support.
758  *
759  * Raises SecurityError exception if $SAFE is 4.
760  */
761 static VALUE
763 {
764  int rows, columns;
765  VALUE res;
766 
767  rb_secure(4);
768  rl_get_screen_size(&rows, &columns);
769  res = rb_ary_new();
770  rb_ary_push(res, INT2NUM(rows));
771  rb_ary_push(res, INT2NUM(columns));
772  return res;
773 }
774 #else
775 #define readline_s_get_screen_size rb_f_notimplement
776 #endif
777 
778 #ifdef HAVE_RL_VI_EDITING_MODE
779 /*
780  * call-seq:
781  * Readline.vi_editing_mode -> nil
782  *
783  * Specifies VI editing mode. See the manual of GNU Readline for
784  * details of VI editing mode.
785  *
786  * Raises NotImplementedError if the using readline library does not support.
787  *
788  * Raises SecurityError exception if $SAFE is 4.
789  */
790 static VALUE
792 {
793  rb_secure(4);
794  rl_vi_editing_mode(1,0);
795  return Qnil;
796 }
797 #else
798 #define readline_s_vi_editing_mode rb_f_notimplement
799 #endif
800 
801 #ifdef HAVE_RL_EDITING_MODE
802 /*
803  * call-seq:
804  * Readline.vi_editing_mode? -> bool
805  *
806  * Returns true if vi mode is active. Returns false if not.
807  *
808  * Raises NotImplementedError if the using readline library does not support.
809  *
810  * Raises SecurityError exception if $SAFE is 4.
811  */
812 static VALUE
814 {
815  rb_secure(4);
816  return rl_editing_mode == 0 ? Qtrue : Qfalse;
817 }
818 #else
819 #define readline_s_vi_editing_mode_p rb_f_notimplement
820 #endif
821 
822 #ifdef HAVE_RL_EMACS_EDITING_MODE
823 /*
824  * call-seq:
825  * Readline.emacs_editing_mode -> nil
826  *
827  * Specifies Emacs editing mode. The default is this mode. See the
828  * manual of GNU Readline for details of Emacs editing mode.
829  *
830  * Raises NotImplementedError if the using readline library does not support.
831  *
832  * Raises SecurityError exception if $SAFE is 4.
833  */
834 static VALUE
836 {
837  rb_secure(4);
838  rl_emacs_editing_mode(1,0);
839  return Qnil;
840 }
841 #else
842 #define readline_s_emacs_editing_mode rb_f_notimplement
843 #endif
844 
845 #ifdef HAVE_RL_EDITING_MODE
846 /*
847  * call-seq:
848  * Readline.emacs_editing_mode? -> bool
849  *
850  * Returns true if emacs mode is active. Returns false if not.
851  *
852  * Raises NotImplementedError if the using readline library does not support.
853  *
854  * Raises SecurityError exception if $SAFE is 4.
855  */
856 static VALUE
858 {
859  rb_secure(4);
860  return rl_editing_mode == 1 ? Qtrue : Qfalse;
861 }
862 #else
863 #define readline_s_emacs_editing_mode_p rb_f_notimplement
864 #endif
865 
866 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
867 /*
868  * call-seq:
869  * Readline.completion_append_character = char
870  *
871  * Specifies a character to be appended on completion.
872  * Nothing will be appended if an empty string ("") or nil is
873  * specified.
874  *
875  * For example:
876  * require "readline"
877  *
878  * Readline.readline("> ", true)
879  * Readline.completion_append_character = " "
880  *
881  * Result:
882  * >
883  * Input "/var/li".
884  *
885  * > /var/li
886  * Press TAB key.
887  *
888  * > /var/lib
889  * Completes "b" and appends " ". So, you can continuously input "/usr".
890  *
891  * > /var/lib /usr
892  *
893  * NOTE: Only one character can be specified. When "string" is
894  * specified, sets only "s" that is the first.
895  *
896  * require "readline"
897  *
898  * Readline.completion_append_character = "string"
899  * p Readline.completion_append_character # => "s"
900  *
901  * Raises NotImplementedError if the using readline library does not support.
902  *
903  * Raises SecurityError exception if $SAFE is 4.
904  */
905 static VALUE
907 {
908  rb_secure(4);
909  if (NIL_P(str)) {
910  rl_completion_append_character = '\0';
911  }
912  else {
913  OutputStringValue(str);
914  if (RSTRING_LEN(str) == 0) {
915  rl_completion_append_character = '\0';
916  } else {
917  rl_completion_append_character = RSTRING_PTR(str)[0];
918  }
919  }
920  return self;
921 }
922 #else
923 #define readline_s_set_completion_append_character rb_f_notimplement
924 #endif
925 
926 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
927 /*
928  * call-seq:
929  * Readline.completion_append_character -> char
930  *
931  * Returns a string containing a character to be appended on
932  * completion. The default is a space (" ").
933  *
934  * Raises NotImplementedError if the using readline library does not support.
935  *
936  * Raises SecurityError exception if $SAFE is 4.
937  */
938 static VALUE
940 {
941  char buf[1];
942 
943  rb_secure(4);
944  if (rl_completion_append_character == '\0')
945  return Qnil;
946 
947  buf[0] = (char) rl_completion_append_character;
948  return rb_locale_str_new(buf, 1);
949 }
950 #else
951 #define readline_s_get_completion_append_character rb_f_notimplement
952 #endif
953 
954 #ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
955 /*
956  * call-seq:
957  * Readline.basic_word_break_characters = string
958  *
959  * Sets the basic list of characters that signal a break between words
960  * for the completer routine. The default is the characters which
961  * break words for completion in Bash: "\t\n\"\\'`@$><=;|&{(".
962  *
963  * Raises NotImplementedError if the using readline library does not support.
964  *
965  * Raises SecurityError exception if $SAFE is 4.
966  */
967 static VALUE
969 {
970  static char *basic_word_break_characters = NULL;
971 
972  rb_secure(4);
973  OutputStringValue(str);
974  if (basic_word_break_characters == NULL) {
975  basic_word_break_characters =
976  ALLOC_N(char, RSTRING_LEN(str) + 1);
977  }
978  else {
979  REALLOC_N(basic_word_break_characters, char, RSTRING_LEN(str) + 1);
980  }
981  strncpy(basic_word_break_characters,
982  RSTRING_PTR(str), RSTRING_LEN(str));
983  basic_word_break_characters[RSTRING_LEN(str)] = '\0';
984  rl_basic_word_break_characters = basic_word_break_characters;
985  return self;
986 }
987 #else
988 #define readline_s_set_basic_word_break_characters rb_f_notimplement
989 #endif
990 
991 #ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
992 /*
993  * call-seq:
994  * Readline.basic_word_break_characters -> string
995  *
996  * Gets the basic list of characters that signal a break between words
997  * for the completer routine.
998  *
999  * Raises NotImplementedError if the using readline library does not support.
1000  *
1001  * Raises SecurityError exception if $SAFE is 4.
1002  */
1003 static VALUE
1005 {
1006  rb_secure(4);
1007  if (rl_basic_word_break_characters == NULL)
1008  return Qnil;
1009  return rb_locale_str_new_cstr(rl_basic_word_break_characters);
1010 }
1011 #else
1012 #define readline_s_get_basic_word_break_characters rb_f_notimplement
1013 #endif
1014 
1015 #ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
1016 /*
1017  * call-seq:
1018  * Readline.completer_word_break_characters = string
1019  *
1020  * Sets the basic list of characters that signal a break between words
1021  * for rl_complete_internal(). The default is the value of
1022  * Readline.basic_word_break_characters.
1023  *
1024  * Raises NotImplementedError if the using readline library does not support.
1025  *
1026  * Raises SecurityError exception if $SAFE is 4.
1027  */
1028 static VALUE
1030 {
1031  static char *completer_word_break_characters = NULL;
1032 
1033  rb_secure(4);
1034  OutputStringValue(str);
1035  if (completer_word_break_characters == NULL) {
1036  completer_word_break_characters =
1037  ALLOC_N(char, RSTRING_LEN(str) + 1);
1038  }
1039  else {
1040  REALLOC_N(completer_word_break_characters, char, RSTRING_LEN(str) + 1);
1041  }
1042  strncpy(completer_word_break_characters,
1043  RSTRING_PTR(str), RSTRING_LEN(str));
1044  completer_word_break_characters[RSTRING_LEN(str)] = '\0';
1045  rl_completer_word_break_characters = completer_word_break_characters;
1046  return self;
1047 }
1048 #else
1049 #define readline_s_set_completer_word_break_characters rb_f_notimplement
1050 #endif
1051 
1052 #ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
1053 /*
1054  * call-seq:
1055  * Readline.completer_word_break_characters -> string
1056  *
1057  * Gets the basic list of characters that signal a break between words
1058  * for rl_complete_internal().
1059  *
1060  * Raises NotImplementedError if the using readline library does not support.
1061  *
1062  * Raises SecurityError exception if $SAFE is 4.
1063  */
1064 static VALUE
1066 {
1067  rb_secure(4);
1068  if (rl_completer_word_break_characters == NULL)
1069  return Qnil;
1070  return rb_locale_str_new_cstr(rl_completer_word_break_characters);
1071 }
1072 #else
1073 #define readline_s_get_completer_word_break_characters rb_f_notimplement
1074 #endif
1075 
1076 #ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
1077 /*
1078  * call-seq:
1079  * Readline.basic_quote_characters = string
1080  *
1081  * Sets a list of quote characters which can cause a word break.
1082  *
1083  * Raises NotImplementedError if the using readline library does not support.
1084  *
1085  * Raises SecurityError exception if $SAFE is 4.
1086  */
1087 static VALUE
1089 {
1090  static char *basic_quote_characters = NULL;
1091 
1092  rb_secure(4);
1093  OutputStringValue(str);
1094  if (basic_quote_characters == NULL) {
1095  basic_quote_characters =
1096  ALLOC_N(char, RSTRING_LEN(str) + 1);
1097  }
1098  else {
1099  REALLOC_N(basic_quote_characters, char, RSTRING_LEN(str) + 1);
1100  }
1101  strncpy(basic_quote_characters,
1102  RSTRING_PTR(str), RSTRING_LEN(str));
1103  basic_quote_characters[RSTRING_LEN(str)] = '\0';
1104  rl_basic_quote_characters = basic_quote_characters;
1105 
1106  return self;
1107 }
1108 #else
1109 #define readline_s_set_basic_quote_characters rb_f_notimplement
1110 #endif
1111 
1112 #ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
1113 /*
1114  * call-seq:
1115  * Readline.basic_quote_characters -> string
1116  *
1117  * Gets a list of quote characters which can cause a word break.
1118  *
1119  * Raises NotImplementedError if the using readline library does not support.
1120  *
1121  * Raises SecurityError exception if $SAFE is 4.
1122  */
1123 static VALUE
1125 {
1126  rb_secure(4);
1127  if (rl_basic_quote_characters == NULL)
1128  return Qnil;
1129  return rb_locale_str_new_cstr(rl_basic_quote_characters);
1130 }
1131 #else
1132 #define readline_s_get_basic_quote_characters rb_f_notimplement
1133 #endif
1134 
1135 #ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
1136 /*
1137  * call-seq:
1138  * Readline.completer_quote_characters = string
1139  *
1140  * Sets a list of characters which can be used to quote a substring of
1141  * the line. Completion occurs on the entire substring, and within
1142  * the substring Readline.completer_word_break_characters are treated
1143  * as any other character, unless they also appear within this list.
1144  *
1145  * Raises NotImplementedError if the using readline library does not support.
1146  *
1147  * Raises SecurityError exception if $SAFE is 4.
1148  */
1149 static VALUE
1151 {
1152  static char *completer_quote_characters = NULL;
1153 
1154  rb_secure(4);
1155  OutputStringValue(str);
1156  if (completer_quote_characters == NULL) {
1157  completer_quote_characters =
1158  ALLOC_N(char, RSTRING_LEN(str) + 1);
1159  }
1160  else {
1161  REALLOC_N(completer_quote_characters, char, RSTRING_LEN(str) + 1);
1162  }
1163  strncpy(completer_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
1164  completer_quote_characters[RSTRING_LEN(str)] = '\0';
1165  rl_completer_quote_characters = completer_quote_characters;
1166 
1167  return self;
1168 }
1169 #else
1170 #define readline_s_set_completer_quote_characters rb_f_notimplement
1171 #endif
1172 
1173 #ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
1174 /*
1175  * call-seq:
1176  * Readline.completer_quote_characters -> string
1177  *
1178  * Gets a list of characters which can be used to quote a substring of
1179  * the line.
1180  *
1181  * Raises NotImplementedError if the using readline library does not support.
1182  *
1183  * Raises SecurityError exception if $SAFE is 4.
1184  */
1185 static VALUE
1187 {
1188  rb_secure(4);
1189  if (rl_completer_quote_characters == NULL)
1190  return Qnil;
1191  return rb_locale_str_new_cstr(rl_completer_quote_characters);
1192 }
1193 #else
1194 #define readline_s_get_completer_quote_characters rb_f_notimplement
1195 #endif
1196 
1197 #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1198 /*
1199  * call-seq:
1200  * Readline.filename_quote_characters = string
1201  *
1202  * Sets a list of characters that cause a filename to be quoted by the completer
1203  * when they appear in a completed filename. The default is nil.
1204  *
1205  * Raises NotImplementedError if the using readline library does not support.
1206  *
1207  * Raises SecurityError exception if $SAFE is 4.
1208  */
1209 static VALUE
1211 {
1212  static char *filename_quote_characters = NULL;
1213 
1214  rb_secure(4);
1215  OutputStringValue(str);
1216  if (filename_quote_characters == NULL) {
1217  filename_quote_characters =
1218  ALLOC_N(char, RSTRING_LEN(str) + 1);
1219  }
1220  else {
1221  REALLOC_N(filename_quote_characters, char, RSTRING_LEN(str) + 1);
1222  }
1223  strncpy(filename_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
1224  filename_quote_characters[RSTRING_LEN(str)] = '\0';
1225  rl_filename_quote_characters = filename_quote_characters;
1226 
1227  return self;
1228 }
1229 #else
1230 #define readline_s_set_filename_quote_characters rb_f_notimplement
1231 #endif
1232 
1233 #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1234 /*
1235  * call-seq:
1236  * Readline.filename_quote_characters -> string
1237  *
1238  * Gets a list of characters that cause a filename to be quoted by the completer
1239  * when they appear in a completed filename.
1240  *
1241  * Raises NotImplementedError if the using readline library does not support.
1242  *
1243  * Raises SecurityError exception if $SAFE is 4.
1244  */
1245 static VALUE
1247 {
1248  rb_secure(4);
1249  if (rl_filename_quote_characters == NULL)
1250  return Qnil;
1251  return rb_locale_str_new_cstr(rl_filename_quote_characters);
1252 }
1253 #else
1254 #define readline_s_get_filename_quote_characters rb_f_notimplement
1255 #endif
1256 
1257 #ifdef HAVE_RL_REFRESH_LINE
1258 /*
1259  * call-seq:
1260  * Readline.refresh_line -> nil
1261  *
1262  * Clear the current input line.
1263  *
1264  * Raises SecurityError exception if $SAFE is 4.
1265  */
1266 static VALUE
1268 {
1269  rb_secure(4);
1270  rl_refresh_line(0, 0);
1271  return Qnil;
1272 }
1273 #else
1274 #define readline_s_refresh_line rb_f_notimplement
1275 #endif
1276 
1277 static VALUE
1279 {
1280  return rb_str_new_cstr("HISTORY");
1281 }
1282 
1283 static int
1285 {
1286  return history_base + offset;
1287 }
1288 
1289 static int
1291 {
1292  return offset;
1293 }
1294 
1295 static VALUE
1297 {
1298  HIST_ENTRY *entry = NULL;
1299  int i;
1300 
1301  rb_secure(4);
1302  i = NUM2INT(index);
1303  if (i < 0) {
1304  i += history_length;
1305  }
1306  if (i >= 0) {
1307  entry = history_get(history_get_offset_func(i));
1308  }
1309  if (entry == NULL) {
1310  rb_raise(rb_eIndexError, "invalid index");
1311  }
1312  return rb_locale_str_new_cstr(entry->line);
1313 }
1314 
1315 #ifdef HAVE_REPLACE_HISTORY_ENTRY
1316 static VALUE
1318 {
1319  HIST_ENTRY *entry = NULL;
1320  int i;
1321 
1322  rb_secure(4);
1323  i = NUM2INT(index);
1324  OutputStringValue(str);
1325  if (i < 0) {
1326  i += history_length;
1327  }
1328  if (i >= 0) {
1329  entry = replace_history_entry(history_replace_offset_func(i), RSTRING_PTR(str), NULL);
1330  }
1331  if (entry == NULL) {
1332  rb_raise(rb_eIndexError, "invalid index");
1333  }
1334  return str;
1335 }
1336 #else
1337 #define hist_set rb_f_notimplement
1338 #endif
1339 
1340 static VALUE
1342 {
1343  rb_secure(4);
1344  OutputStringValue(str);
1345  add_history(RSTRING_PTR(str));
1346  return self;
1347 }
1348 
1349 static VALUE
1351 {
1352  VALUE str;
1353 
1354  rb_secure(4);
1355  while (argc--) {
1356  str = *argv++;
1357  OutputStringValue(str);
1358  add_history(RSTRING_PTR(str));
1359  }
1360  return self;
1361 }
1362 
1363 static VALUE
1365 {
1366 #ifdef HAVE_REMOVE_HISTORY
1367  HIST_ENTRY *entry;
1368  VALUE val;
1369 
1370  rb_secure(4);
1371  entry = remove_history(index);
1372  if (entry) {
1373  val = rb_locale_str_new_cstr(entry->line);
1374  free((void *) entry->line);
1375  free(entry);
1376  return val;
1377  }
1378  return Qnil;
1379 #else
1380  rb_notimplement();
1381  return Qnil; /* not reached */
1382 #endif
1383 }
1384 
1385 static VALUE
1387 {
1388  rb_secure(4);
1389  if (history_length > 0) {
1390  return rb_remove_history(history_length - 1);
1391  } else {
1392  return Qnil;
1393  }
1394 }
1395 
1396 static VALUE
1398 {
1399  rb_secure(4);
1400  if (history_length > 0) {
1401  return rb_remove_history(0);
1402  } else {
1403  return Qnil;
1404  }
1405 }
1406 
1407 static VALUE
1409 {
1410  HIST_ENTRY *entry;
1411  int i;
1412 
1413  RETURN_ENUMERATOR(self, 0, 0);
1414 
1415  rb_secure(4);
1416  for (i = 0; i < history_length; i++) {
1417  entry = history_get(history_get_offset_func(i));
1418  if (entry == NULL)
1419  break;
1420  rb_yield(rb_locale_str_new_cstr(entry->line));
1421  }
1422  return self;
1423 }
1424 
1425 static VALUE
1427 {
1428  rb_secure(4);
1429  return INT2NUM(history_length);
1430 }
1431 
1432 static VALUE
1434 {
1435  rb_secure(4);
1436  return history_length == 0 ? Qtrue : Qfalse;
1437 }
1438 
1439 static VALUE
1441 {
1442  int i;
1443 
1444  rb_secure(4);
1445  i = NUM2INT(index);
1446  if (i < 0)
1447  i += history_length;
1448  if (i < 0 || i > history_length - 1) {
1449  rb_raise(rb_eIndexError, "invalid index");
1450  }
1451  return rb_remove_history(i);
1452 }
1453 
1454 #ifdef HAVE_CLEAR_HISTORY
1455 static VALUE
1456 hist_clear(VALUE self)
1457 {
1458  rb_secure(4);
1459  clear_history();
1460  return self;
1461 }
1462 #else
1463 #define hist_clear rb_f_notimplement
1464 #endif
1465 
1466 static VALUE
1468 {
1469  VALUE result;
1470  char **matches;
1471  int i;
1472 
1473  matches = rl_completion_matches(StringValuePtr(str),
1475  if (matches) {
1476  result = rb_ary_new();
1477  for (i = 0; matches[i]; i++) {
1478  rb_ary_push(result, rb_locale_str_new_cstr(matches[i]));
1479  free(matches[i]);
1480  }
1481  free(matches);
1482  if (RARRAY_LEN(result) >= 2)
1483  rb_ary_shift(result);
1484  }
1485  else {
1486  result = Qnil;
1487  }
1488  return result;
1489 }
1490 
1491 static VALUE
1493 {
1494  VALUE result;
1495  char **matches;
1496  int i;
1497 
1498  matches = rl_completion_matches(StringValuePtr(str),
1500  if (matches) {
1501  result = rb_ary_new();
1502  for (i = 0; matches[i]; i++) {
1503  rb_ary_push(result, rb_locale_str_new_cstr(matches[i]));
1504  free(matches[i]);
1505  }
1506  free(matches);
1507  if (RARRAY_LEN(result) >= 2)
1508  rb_ary_shift(result);
1509  }
1510  else {
1511  result = Qnil;
1512  }
1513  return result;
1514 }
1515 
1516 void
1518 {
1519  VALUE history, fcomp, ucomp, version;
1520 
1521  /* Allow conditional parsing of the ~/.inputrc file. */
1522  rl_readline_name = (char *)"Ruby";
1523 
1524 #if defined HAVE_RL_GETC_FUNCTION
1525  /* libedit check rl_getc_function only when rl_initialize() is called, */
1526  /* and using_history() call rl_initialize(). */
1527  /* This assignment should be placed before using_history() */
1528  rl_getc_function = readline_getc;
1529  id_getbyte = rb_intern_const("getbyte");
1530 #elif defined HAVE_RL_EVENT_HOOK
1531  rl_event_hook = readline_event;
1532 #endif
1533 
1534  using_history();
1535 
1538 
1539  mReadline = rb_define_module("Readline");
1541  readline_readline, -1);
1546  rb_define_singleton_method(mReadline, "completion_proc=",
1548  rb_define_singleton_method(mReadline, "completion_proc",
1550  rb_define_singleton_method(mReadline, "completion_case_fold=",
1552  rb_define_singleton_method(mReadline, "completion_case_fold",
1554  rb_define_singleton_method(mReadline, "line_buffer",
1558  rb_define_singleton_method(mReadline, "set_screen_size",
1560  rb_define_singleton_method(mReadline, "get_screen_size",
1562  rb_define_singleton_method(mReadline, "vi_editing_mode",
1564  rb_define_singleton_method(mReadline, "vi_editing_mode?",
1566  rb_define_singleton_method(mReadline, "emacs_editing_mode",
1568  rb_define_singleton_method(mReadline, "emacs_editing_mode?",
1570  rb_define_singleton_method(mReadline, "completion_append_character=",
1572  rb_define_singleton_method(mReadline, "completion_append_character",
1574  rb_define_singleton_method(mReadline, "basic_word_break_characters=",
1576  rb_define_singleton_method(mReadline, "basic_word_break_characters",
1578  rb_define_singleton_method(mReadline, "completer_word_break_characters=",
1580  rb_define_singleton_method(mReadline, "completer_word_break_characters",
1582  rb_define_singleton_method(mReadline, "basic_quote_characters=",
1584  rb_define_singleton_method(mReadline, "basic_quote_characters",
1586  rb_define_singleton_method(mReadline, "completer_quote_characters=",
1588  rb_define_singleton_method(mReadline, "completer_quote_characters",
1590  rb_define_singleton_method(mReadline, "filename_quote_characters=",
1592  rb_define_singleton_method(mReadline, "filename_quote_characters",
1594  rb_define_singleton_method(mReadline, "refresh_line",
1596 
1597 #if USE_INSERT_IGNORE_ESCAPE
1598  CONST_ID(id_orig_prompt, "orig_prompt");
1599  CONST_ID(id_last_prompt, "last_prompt");
1600 #endif
1601 
1602  history = rb_obj_alloc(rb_cObject);
1603  rb_extend_object(history, rb_mEnumerable);
1604  rb_define_singleton_method(history,"to_s", hist_to_s, 0);
1605  rb_define_singleton_method(history,"[]", hist_get, 1);
1606  rb_define_singleton_method(history,"[]=", hist_set, 2);
1607  rb_define_singleton_method(history,"<<", hist_push, 1);
1608  rb_define_singleton_method(history,"push", hist_push_method, -1);
1609  rb_define_singleton_method(history,"pop", hist_pop, 0);
1610  rb_define_singleton_method(history,"shift", hist_shift, 0);
1611  rb_define_singleton_method(history,"each", hist_each, 0);
1612  rb_define_singleton_method(history,"length", hist_length, 0);
1613  rb_define_singleton_method(history,"size", hist_length, 0);
1614  rb_define_singleton_method(history,"empty?", hist_empty_p, 0);
1615  rb_define_singleton_method(history,"delete_at", hist_delete_at, 1);
1616  rb_define_singleton_method(history,"clear", hist_clear, 0);
1617 
1618  /*
1619  * The history buffer. It extends Enumerable module, so it behaves
1620  * just like an array.
1621  * For example, gets the fifth content that the user input by
1622  * HISTORY[4].
1623  */
1624  rb_define_const(mReadline, "HISTORY", history);
1625 
1626  fcomp = rb_obj_alloc(rb_cObject);
1627  rb_define_singleton_method(fcomp, "call",
1629  /*
1630  * The Object with the call method that is a completion for filename.
1631  * This is sets by Readline.completion_proc= method.
1632  */
1633  rb_define_const(mReadline, "FILENAME_COMPLETION_PROC", fcomp);
1634 
1635  ucomp = rb_obj_alloc(rb_cObject);
1636  rb_define_singleton_method(ucomp, "call",
1638  /*
1639  * The Object with the call method that is a completion for usernames.
1640  * This is sets by Readline.completion_proc= method.
1641  */
1642  rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
1645 #if defined HAVE_RL_LIBRARY_VERSION
1646  version = rb_str_new_cstr(rl_library_version);
1647 #if defined HAVE_CLEAR_HISTORY || defined HAVE_REMOVE_HISTORY
1648  if (strncmp(rl_library_version, EDIT_LINE_LIBRARY_VERSION,
1650  add_history("1");
1651  if (history_get(history_get_offset_func(0)) == NULL) {
1653  }
1654 #ifdef HAVE_REPLACE_HISTORY_ENTRY
1655  if (replace_history_entry(0, "a", NULL) == NULL) {
1657  }
1658 #endif
1659 #ifdef HAVE_CLEAR_HISTORY
1660  clear_history();
1661 #else
1662  {
1663  HIST_ENTRY *entry = remove_history(0);
1664  if (entry) {
1665  free((char *)entry->line);
1666  free(entry);
1667  }
1668  }
1669 #endif
1670  }
1671 #endif
1672 #else
1673  version = rb_str_new_cstr("2.0 or prior version");
1674 #endif
1675  /* Version string of GNU Readline or libedit. */
1676  rb_define_const(mReadline, "VERSION", version);
1677 
1678  rl_attempted_completion_function = readline_attempted_completion_function;
1679 #ifdef HAVE_RL_CATCH_SIGNALS
1680  rl_catch_signals = 0;
1681 #endif
1682 
1683 #ifdef HAVE_RL_CLEAR_SIGNALS
1684  rl_clear_signals();
1685 #endif
1686 
1688 }
static ID completion_case_fold
Definition: readline.c:60
#define RSTRING_LEN(string)
Definition: generator.h:45
RARRAY_PTR(q->result)[0]
ssize_t n
Definition: bigdecimal.c:5519
volatile VALUE ary
Definition: tcltklib.c:9700
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Definition: encoding.c:739
#define readline_s_get_line_buffer
Definition: readline.c:624
VP_EXPORT int
Definition: bigdecimal.c:4911
#define readline_s_get_basic_quote_characters
Definition: readline.c:1132
size_t strlen(const char *)
int rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
static VALUE readline_get(VALUE prompt)
Definition: readline.c:266
#define NUM2INT(x)
Definition: ruby.h:536
parser parser_yylval val
Definition: ripper.c:14289
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1343
static VALUE mReadline
Definition: readline.c:47
VALUE proc
Definition: tcltklib.c:2948
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:1889
static VALUE readline_s_set_input(VALUE self, VALUE input)
Definition: readline.c:436
Definition: io.h:53
static VALUE hist_to_s(VALUE self)
Definition: readline.c:1278
VALUE rb_ary_shift(VALUE ary)
Definition: array.c:832
static char ** readline_attempted_completion_function(const char *text, int start, int end)
Definition: readline.c:652
#define readline_s_set_completer_word_break_characters
Definition: readline.c:1049
#define readline_s_emacs_editing_mode
Definition: readline.c:842
VALUE rb_str_unlocktmp(VALUE)
Definition: string.c:1746
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:102
static ID completion_proc
Definition: readline.c:60
int status
Definition: tcltklib.c:2186
RUBY_EXTERN VALUE rb_stdin
Definition: ruby.h:1320
unsigned long VALUE
Definition: ruby.h:88
VALUE enc
Definition: tcltklib.c:10402
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:740
static VALUE INT2NUM(int v)
Definition: ruby.h:981
static void VALUE * p2
Definition: array.c:1848
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:704
#define RSTRING_PTR(string)
Definition: generator.h:42
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
Definition: encoding.c:894
#define Check_Type(v, t)
Definition: ruby.h:459
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
return Qtrue
Definition: tcltklib.c:9597
int index
Definition: tcltklib.c:4467
#define RARRAY_LEN(ARRAY)
Definition: generator.h:39
#define COMPLETION_CASE_FOLD
Definition: readline.c:59
return str
Definition: ruby.c:1183
static VALUE hist_pop(VALUE self)
Definition: readline.c:1386
static VALUE readline_s_get_completion_case_fold(VALUE self)
Definition: readline.c:597
#define T_ARRAY
Definition: ruby.h:420
static VALUE readline_s_set_output(VALUE self, VALUE output)
Definition: readline.c:460
VALUE rb_str_tmp_new(long)
#define GetOpenFile(obj, fp)
Definition: io.h:110
static int history_get_offset_history_base(int offset)
Definition: readline.c:1284
static VALUE hist_push(VALUE self, VALUE str)
Definition: readline.c:1341
rb_thread_schedule()
Definition: thread.c:1048
#define readline_s_vi_editing_mode_p
Definition: readline.c:819
rb_secure(4)
#define OutputStringValue(str)
Definition: readline.c:84
#define ISALPHA(c)
Definition: ruby.h:1457
static VALUE hist_length(VALUE self)
Definition: readline.c:1426
static unsigned char * output
Definition: nkf.c:32
#define Qnil
Definition: ruby.h:367
static VALUE filename_completion_proc_call(VALUE self, VALUE str)
Definition: readline.c:1467
static VALUE hist_each(VALUE self)
Definition: readline.c:1408
strcpy(cmd2, cmd)
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1353
#define rl_filename_completion_function
Definition: readline.c:66
n NULL
Definition: yaml2byte.c:134
#define readline_s_set_completer_quote_characters
Definition: readline.c:1170
VALUE rb_Array(VALUE)
Definition: object.c:2469
unsigned int input
Definition: nkf.c:3916
return Qfalse
Definition: tcltklib.c:6768
#define ALLOC_N(type, n)
Definition: ruby.h:1034
VALUE rb_str_new_shared(VALUE)
Definition: string.c:636
#define readline_s_get_basic_word_break_characters
Definition: readline.c:1012
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1246
#define COMPLETION_PROC
Definition: readline.c:58
#define EDIT_LINE_LIBRARY_VERSION
Definition: readline.c:49
VALUE rb_obj_as_string(VALUE)
Definition: string.c:854
#define readline_s_refresh_line
Definition: readline.c:1274
VALUE rb_ary_new(void)
Definition: array.c:339
static VALUE username_completion_proc_call(VALUE self, VALUE str)
Definition: readline.c:1492
VALUE rb_locale_str_new(const char *, long)
Definition: string.c:561
#define NIL_P(v)
Definition: ruby.h:374
static VALUE hist_shift(VALUE self)
Definition: readline.c:1397
#define readline_s_get_filename_quote_characters
Definition: readline.c:1254
int fd
Definition: io.h:54
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:1923
#define readline_s_vi_editing_mode
Definition: readline.c:798
VALUE rb_eNoMemError
Definition: error.c:478
#define readline_s_emacs_editing_mode_p
Definition: readline.c:863
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1601
VALUE rb_eIndexError
Definition: error.c:469
static VALUE hist_push_method(int argc, VALUE *argv, VALUE self)
Definition: readline.c:1350
#define EOF
Definition: vsnprintf.c:206
VALUE * argv
Definition: tcltklib.c:1962
VALUE rb_locale_str_new_cstr(const char *)
Definition: string.c:567
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1358
VALUE rb_yield(VALUE)
Definition: vm_eval.c:781
#define REALLOC_N(var, type, n)
Definition: ruby.h:1036
int errno
q result
Definition: tcltklib.c:7059
VALUE rb_mEnumerable
Definition: enum.c:17
register char * s
Definition: os2.c:56
#define malloc
Definition: ripper.c:94
#define strdup(s)
Definition: util.h:69
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
unsigned long ID
Definition: ruby.h:89
static int history_get_offset_0(int offset)
Definition: readline.c:1290
int argc
Definition: tcltklib.c:1961
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1171
static VALUE readline_s_set_completion_proc(VALUE self, VALUE proc)
Definition: readline.c:542
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:888
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:2965
register unsigned int len
Definition: name2ctype.h:22210
VALUE rb_str_new_cstr(const char *)
Definition: string.c:432
static int(* history_get_offset_func)(int)
Definition: readline.c:75
#define readline_s_get_point
Definition: readline.c:648
void rb_jump_tag(int tag)
Definition: eval.c:598
#define free(x)
Definition: dln.c:50
VpDivd * c
Definition: bigdecimal.c:1163
static VALUE hist_empty_p(VALUE self)
Definition: readline.c:1433
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1231
void rb_memerror(void)
Definition: gc.c:581
static VALUE readline_s_set_completion_case_fold(VALUE self, VALUE val)
Definition: readline.c:574
#define StringValueCStr(v)
Definition: ruby.h:468
gz end
Definition: zlib.c:2033
#define readline_s_set_filename_quote_characters
Definition: readline.c:1230
void Init_readline()
Definition: readline.c:1517
#define hist_set
Definition: readline.c:1337
static VALUE readline_s_get_completion_proc(VALUE self)
Definition: readline.c:559
static VALUE rb_remove_history(int index)
Definition: readline.c:1364
#define readline_s_get_completer_quote_characters
Definition: readline.c:1194
char * start
Definition: yaml2byte.c:126
rb_ivar_set(einfo, ID_at_interp, interp)
#define readline_s_set_screen_size
Definition: readline.c:745
static int(* history_replace_offset_func)(int)
Definition: readline.c:76
static VALUE readline_readline(int argc, VALUE *argv, VALUE self)
Definition: readline.c:361
#define RTEST(v)
Definition: ruby.h:373
#define rb_str_set_len(str, length)
Definition: ruby_missing.h:30
VALUE rb_str_locktmp(VALUE)
#define T_FILE
Definition: ruby.h:424
static VALUE hist_get(VALUE self, VALUE index)
Definition: readline.c:1296
void rb_notimplement(void)
Definition: error.c:1598
#define hist_clear
Definition: readline.c:1463
#define rl_username_completion_function
Definition: readline.c:69
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:210
FILE * rb_io_stdio_file(rb_io_t *fptr)
Definition: io.c:6771
#define rl_completion_matches
Definition: readline.c:72
#define readline_s_get_screen_size
Definition: readline.c:775
int rb_tolower(int c)
Definition: encoding.c:1652
#define readline_s_set_basic_word_break_characters
Definition: readline.c:988
Real * res
Definition: bigdecimal.c:1189
static char NUM2CHR(VALUE x)
Definition: ruby.h:1027
RUBY_EXTERN VALUE rb_eIOError
Definition: ruby.h:1296
#define readline_s_get_completion_append_character
Definition: readline.c:951
return rb_funcall(q->proc, ID_call, 0)
#define StringValuePtr(v)
Definition: ruby.h:467
BDIGIT e
Definition: bigdecimal.c:4946
#define fileno(p)
Definition: vsnprintf.c:222
#define CONST_ID(var, str)
Definition: ruby.h:1127
FILE * stdio_file
Definition: io.h:55
static void version(void)
Definition: nkf.c:874
#define rb_intern_const(str)
Definition: ruby.h:1141
#define RB_WAITFD_IN
Definition: io.h:37
ssize_t i
Definition: bigdecimal.c:5519
VALUE rb_define_module(const char *name)
Definition: class.c:587
#define rb_intern(str)
#define fstat(fd, st)
Definition: win32.h:202
#define stat(path, st)
Definition: win32.h:201
#define readline_s_set_basic_quote_characters
Definition: readline.c:1109
static VALUE hist_delete_at(VALUE self, VALUE index)
Definition: readline.c:1440
VALUE rb_eArgError
Definition: error.c:468
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1032
#define readline_s_get_completer_word_break_characters
Definition: readline.c:1073
#define readline_s_set_completion_append_character
Definition: readline.c:923