1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/readline/readline.c: supported libedit. fixed: [ruby-core:4858]

* ext/readline/extconf.rb: added new option --enable-libedit.

* test/readline/test_readline.rb: added assertions for
  Readline::HISTORY.

* lib/irb/input-method.rb: do not use Readline::HISTORY.pop.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8489 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2005-05-19 14:36:20 +00:00
parent ddec3de32a
commit 3fd16970f3
5 changed files with 173 additions and 124 deletions

View file

@ -1,3 +1,14 @@
Thu May 19 23:33:09 2005 Shugo Maeda <shugo@ruby-lang.org>
* ext/readline/readline.c: supported libedit. fixed: [ruby-core:4858]
* ext/readline/extconf.rb: added new option --enable-libedit.
* test/readline/test_readline.rb: added assertions for
Readline::HISTORY.
* lib/irb/input-method.rb: do not use Readline::HISTORY.pop.
Wed May 18 23:42:25 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (exc_exception): reverted to call Exception#initialize

View file

@ -1,30 +1,60 @@
require "mkmf"
$readline_headers = ["stdio.h"]
def have_readline_header(header)
if have_header(header)
$readline_headers.push(header)
return true
else
return false
end
end
def have_readline_var(var)
return have_var(var, $readline_headers)
end
dir_config('curses')
dir_config('ncurses')
dir_config('termcap')
dir_config("readline")
enable_libedit = enable_config("libedit")
have_library("user32", nil) if /cygwin/ === RUBY_PLATFORM
have_library("ncurses", "tgetnum") or
have_library("termcap", "tgetnum") or
have_library("ncurses", "tgetnum") ||
have_library("termcap", "tgetnum") ||
have_library("curses", "tgetnum")
if have_header("readline/readline.h") and
have_header("readline/history.h") and
have_library("readline", "readline")
if have_func("rl_filename_completion_function")
$CFLAGS += " -DREADLINE_42_OR_LATER"
if enable_libedit
unless (have_readline_header("editline/readline.h") ||
have_readline_header("readline/readline.h")) &&
have_library("edit", "readline")
exit
end
if have_func("rl_cleanup_after_signal")
$CFLAGS += " -DREADLINE_40_OR_LATER"
else
unless ((have_readline_header("readline/readline.h") &&
have_readline_header("readline/history.h")) &&
(have_library("readline", "readline") ||
have_library("edit", "readline"))) ||
(have_readline_header("editline/readline.h") &&
have_library("edit", "readline"))
exit
end
if try_link(<<EOF, $libs)
#include <stdio.h>
#include <readline/readline.h>
main() {rl_completion_append_character = 1;}
EOF
# this feature is implemented in readline-2.1 or later.
$CFLAGS += " -DREADLINE_21_OR_LATER"
end
create_makefile("readline")
end
have_readline_var("rl_filename_completion_function")
have_readline_var("rl_deprep_term_function")
have_readline_var("rl_completion_append_character")
have_readline_var("rl_basic_word_break_characters")
have_readline_var("rl_completer_word_break_characters")
have_readline_var("rl_basic_quote_characters")
have_readline_var("rl_completer_quote_characters")
have_readline_var("rl_filename_quote_characters")
have_readline_var("rl_attempted_completion_over")
have_readline_var("rl_library_version")
have_readline_var("rl_event_hook")
have_func("rl_cleanup_after_signal")
have_func("rl_clear_signals")
have_func("replace_history_entry")
have_func("remove_history")
create_makefile("readline")

View file

@ -3,8 +3,16 @@
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_READLINE_READLINE_H
#include <readline/readline.h>
#endif
#ifdef HAVE_READLINE_HISTORY_H
#include <readline/history.h>
#endif
#ifdef HAVE_EDITLINE_READLINE_H
#include <editline/readline.h>
#endif
#include "ruby.h"
#include "rubyio.h"
@ -22,12 +30,16 @@ static VALUE mReadline;
#define COMPLETION_CASE_FOLD "completion_case_fold"
static ID completion_proc, completion_case_fold;
#ifndef READLINE_42_OR_LATER
#ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
# define rl_filename_completion_function filename_completion_function
# define rl_username_completion_function username_completion_function
# define rl_completion_matches completion_matches
#endif
static int readline_event(void);
static char **readline_attempted_completion_function(const char *text,
int start, int end);
static int
readline_event()
{
@ -63,10 +75,10 @@ readline_readline(argc, argv, self)
buff = (char*)rb_protect((VALUE(*)_((VALUE)))readline, (VALUE)prompt,
&status);
if (status) {
#if defined READLINE_40_OR_LATER
#if defined HAVE_RL_CLEANUP_AFTER_SIGNAL
/* restore terminal mode and signal handler*/
rl_cleanup_after_signal();
#elif defined READLINE_21_OR_LATER
#elif defined HAVE_RL_DEPREP_TERM_FUNCTION
/* restore terminal mode */
(*rl_deprep_term_function)();
#else
@ -124,7 +136,7 @@ readline_s_get_completion_case_fold(self)
static char **
readline_attempted_completion_function(text, start, end)
char *text;
const char *text;
int start;
int end;
{
@ -136,7 +148,9 @@ readline_attempted_completion_function(text, start, end)
proc = rb_attr_get(mReadline, completion_proc);
if (NIL_P(proc))
return NULL;
#ifdef HAVE_RL_ATTEMPTED_COMPLETION_OVER
rl_attempted_completion_over = 1;
#endif
case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold));
ary = rb_funcall(proc, rb_intern("call"), 1, rb_tainted_str_new2(text));
if (TYPE(ary) != T_ARRAY)
@ -209,7 +223,7 @@ static VALUE
readline_s_set_completion_append_character(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
rb_secure(4);
if (NIL_P(str)) {
rl_completion_append_character = '\0';
@ -226,14 +240,14 @@ readline_s_set_completion_append_character(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */
}
static VALUE
readline_s_get_completion_append_character(self)
VALUE self;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
VALUE str;
rb_secure(4);
@ -246,14 +260,14 @@ readline_s_get_completion_append_character(self)
return str;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */
}
static VALUE
readline_s_set_basic_word_break_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
static char *basic_word_break_characters = NULL;
rb_secure(4);
@ -273,28 +287,28 @@ readline_s_set_basic_word_break_characters(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */
}
static VALUE
readline_s_get_basic_word_break_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
rb_secure(4);
if (rl_basic_word_break_characters == NULL)
return Qnil;
return rb_tainted_str_new2(rl_basic_word_break_characters);
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */
}
static VALUE
readline_s_set_completer_word_break_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
static char *completer_word_break_characters = NULL;
rb_secure(4);
@ -314,28 +328,28 @@ readline_s_set_completer_word_break_characters(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */
}
static VALUE
readline_s_get_completer_word_break_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
rb_secure(4);
if (rl_completer_word_break_characters == NULL)
return Qnil;
return rb_tainted_str_new2(rl_completer_word_break_characters);
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */
}
static VALUE
readline_s_set_basic_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
static char *basic_quote_characters = NULL;
rb_secure(4);
@ -355,28 +369,28 @@ readline_s_set_basic_quote_characters(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */
}
static VALUE
readline_s_get_basic_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
rb_secure(4);
if (rl_basic_quote_characters == NULL)
return Qnil;
return rb_tainted_str_new2(rl_basic_quote_characters);
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */
}
static VALUE
readline_s_set_completer_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
static char *completer_quote_characters = NULL;
rb_secure(4);
@ -396,28 +410,28 @@ readline_s_set_completer_quote_characters(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */
}
static VALUE
readline_s_get_completer_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
rb_secure(4);
if (rl_completer_quote_characters == NULL)
return Qnil;
return rb_tainted_str_new2(rl_completer_quote_characters);
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */
}
static VALUE
readline_s_set_filename_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
static char *filename_quote_characters = NULL;
rb_secure(4);
@ -437,39 +451,21 @@ readline_s_set_filename_quote_characters(self, str)
return self;
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
#endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */
}
static VALUE
readline_s_get_filename_quote_characters(self, str)
VALUE self, str;
{
#ifdef READLINE_21_OR_LATER
#ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
rb_secure(4);
if (rl_filename_quote_characters == NULL)
return Qnil;
return rb_tainted_str_new2(rl_filename_quote_characters);
#else
rb_notimplement();
#endif /* READLINE_21_OR_LATER */
}
static VALUE
rb_remove_history(index)
int index;
{
HIST_ENTRY *entry;
VALUE val;
rb_secure(4);
entry = remove_history(index);
if (entry) {
val = rb_tainted_str_new2(entry->line);
free(entry->line);
free(entry);
return val;
}
return Qnil;
#endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */
}
static VALUE
@ -484,19 +480,19 @@ hist_get(self, index)
VALUE self;
VALUE index;
{
HISTORY_STATE *state;
HIST_ENTRY *entry;
int i;
rb_secure(4);
state = history_get_history_state();
i = NUM2INT(index);
if (i < 0) {
i += state->length;
i += history_length;
}
if (i < 0 || i > state->length - 1) {
entry = history_get(history_base + i);
if (entry == NULL) {
rb_raise(rb_eIndexError, "invalid index");
}
return rb_tainted_str_new2(state->entries[i]->line);
return rb_tainted_str_new2(entry->line);
}
static VALUE
@ -505,21 +501,23 @@ hist_set(self, index, str)
VALUE index;
VALUE str;
{
HISTORY_STATE *state;
VALUE s = str;
#ifdef HAVE_REPLACE_HISTORY_ENTRY
HIST_ENTRY *entry;
int i;
rb_secure(4);
state = history_get_history_state();
i = NUM2INT(index);
SafeStringValue(str);
if (i < 0) {
i += state->length;
i += history_length;
}
if (i < 0 || i > state->length - 1) {
entry = replace_history_entry(i, RSTRING(str)->ptr, NULL);
if (entry == NULL) {
rb_raise(rb_eIndexError, "invalid index");
}
SafeStringValue(str);
replace_history_entry(i, RSTRING(str)->ptr, NULL);
#else
rb_notimplement();
#endif
return str;
}
@ -551,16 +549,35 @@ hist_push_method(argc, argv, self)
return self;
}
static VALUE
rb_remove_history(index)
int index;
{
#ifdef HAVE_REMOVE_HISTORY
HIST_ENTRY *entry;
VALUE val;
rb_secure(4);
entry = remove_history(index);
if (entry) {
val = rb_tainted_str_new2(entry->line);
free(entry->line);
free(entry);
return val;
}
#else
rb_notimplement();
#endif
return Qnil;
}
static VALUE
hist_pop(self)
VALUE self;
{
HISTORY_STATE *state;
rb_secure(4);
state = history_get_history_state();
if (state->length > 0) {
return rb_remove_history(state->length - 1);
if (history_length > 0) {
return rb_remove_history(history_length - 1);
} else {
return Qnil;
}
@ -570,11 +587,8 @@ static VALUE
hist_shift(self)
VALUE self;
{
HISTORY_STATE *state;
rb_secure(4);
state = history_get_history_state();
if (state->length > 0) {
if (history_length > 0) {
return rb_remove_history(0);
} else {
return Qnil;
@ -585,13 +599,15 @@ static VALUE
hist_each(self)
VALUE self;
{
HISTORY_STATE *state;
HIST_ENTRY *entry;
int i;
rb_secure(4);
state = history_get_history_state();
for (i = 0; i < state->length; i++) {
rb_yield(rb_tainted_str_new2(state->entries[i]->line));
for (i = 0; i < history_length; i++) {
entry = history_get(history_base + i);
if (entry == NULL)
break;
rb_yield(rb_tainted_str_new2(entry->line));
}
return self;
}
@ -600,25 +616,16 @@ static VALUE
hist_length(self)
VALUE self;
{
HISTORY_STATE *state;
rb_secure(4);
state = history_get_history_state();
return INT2NUM(state->length);
return INT2NUM(history_length);
}
static VALUE
hist_empty_p(self)
VALUE self;
{
HISTORY_STATE *state;
rb_secure(4);
state = history_get_history_state();
if (state->length == 0)
return Qtrue;
else
return Qfalse;
return history_length == 0 ? Qtrue : Qfalse;
}
static VALUE
@ -626,15 +633,13 @@ hist_delete_at(self, index)
VALUE self;
VALUE index;
{
HISTORY_STATE *state;
int i;
rb_secure(4);
state = history_get_history_state();
i = NUM2INT(index);
if (i < 0)
i += state->length;
if (i < 0 || i > state->length - 1) {
i += history_length;
if (i < 0 || i > history_length - 1) {
rb_raise(rb_eIndexError, "invalid index");
}
return rb_remove_history(i);
@ -759,7 +764,6 @@ Init_readline()
rb_define_singleton_method(history,"each", hist_each, 0);
rb_define_singleton_method(history,"length", hist_length, 0);
rb_define_singleton_method(history,"size", hist_length, 0);
rb_define_singleton_method(history,"empty?", hist_empty_p, 0);
rb_define_singleton_method(history,"delete_at", hist_delete_at, 1);
rb_define_const(mReadline, "HISTORY", history);
@ -773,21 +777,18 @@ Init_readline()
rb_define_singleton_method(ucomp, "call",
username_completion_proc_call, 1);
rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
#if defined READLINE_21_OR_LATER
#if defined HAVE_RL_LIBRARY_VERSION
rb_define_const(mReadline, "VERSION", rb_str_new2(rl_library_version));
#else
rb_define_const(mReadline, "VERSION",
rb_str_new2("2.0 or before version"));
#endif
#if defined READLINE_42_OR_LATER
rl_attempted_completion_function
= (rl_completion_func_t *)readline_attempted_completion_function;
rl_event_hook = (rl_hook_func_t *)readline_event;
#else
rl_attempted_completion_function
= (CPPFunction *) readline_attempted_completion_function;
rl_attempted_completion_function = readline_attempted_completion_function;
#ifdef HAVE_RL_EVENT_HOOK
rl_event_hook = readline_event;
#endif
#ifdef HAVE_RL_CLEAR_SIGNALS
rl_clear_signals();
#endif
}

View file

@ -94,8 +94,8 @@ module IRB
end
def gets
if l = readline(@prompt, true)
HISTORY.pop if l.empty?
if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true

View file

@ -16,11 +16,15 @@ class TestReadline < Test::Unit::TestCase
stdin.write("hello\n")
stdin.close
stdout.close
line = replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
line = replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", true)
}
assert_equal("hello", line)
assert_equal(true, line.tainted?)
stdout.open
assert_equal("> ", stdout.read(2))
assert_equal(1, Readline::HISTORY.length)
assert_equal("hello", Readline::HISTORY[0])
assert_raises(SecurityError) do
Thread.start {
$SAFE = 1
@ -42,14 +46,17 @@ class TestReadline < Test::Unit::TestCase
end
def test_completion_append_character
Readline.completion_append_character = "x"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = "xyz"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = nil
assert_equal(nil, Readline.completion_append_character)
Readline.completion_append_character = ""
assert_equal(nil, Readline.completion_append_character)
begin
Readline.completion_append_character = "x"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = "xyz"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = nil
assert_equal(nil, Readline.completion_append_character)
Readline.completion_append_character = ""
assert_equal(nil, Readline.completion_append_character)
rescue NotImplementedError
end
end
private