Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use FFI ~> 0.6.0 and attach_variable to get direct access to rl_line_buffer. #9

Closed
wants to merge 4 commits into from

2 participants

@postmodern

Using a C extension to only access a global variable is a bit overkill. Using FFI is easier and allows Bond to work out of the box on JRuby and Rubinius.

@postmodern

Correction, JRuby uses a java based readline. But using FFI instead of C extensions is generally more reliable.

@cldwalker
Owner

Ended up releasing with Charlie's suggestion: https://gist.github.com/749427
Still considering an optional ffi install/usage for windows/rubinius

@postmodern

Excellent. Thank you for getting this fixed so quickly.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  .gemspec
@@ -18,8 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'mocha', '>= 0.9.8'
s.add_development_dependency 'mocha-on-bacon'
s.add_development_dependency 'bacon-bits'
- s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} **/deps.rip]) + %w{Rakefile .gemspec}
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
- s.extensions = ["ext/readline_line_buffer/extconf.rb"]
s.license = 'MIT'
end
View
22 ext/readline_line_buffer/extconf.rb
@@ -1,22 +0,0 @@
-require "mkmf"
-
-# placate rubygems when running `make install`
-def dummy_makefile
- File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") {|f|
- f.puts %[install:\n\techo "This is a dummy extension"]
- }
-end
-
-if RUBY_VERSION < '1.9.2'
- dir_config("readline")
- have_library('readline')
- if !have_header('readline/readline.h')
- puts "Bond was built without readline. To use it with readline: gem install bond" +
- " -- --with-readline-dir=/path/to/readline"
- dummy_makefile
- else
- create_makefile 'readline_line_buffer'
- end
-else
- dummy_makefile
-end
View
24 ext/readline_line_buffer/readline_line_buffer.c
@@ -1,24 +0,0 @@
-/* readline.c -- GNU Readline module
- Copyright (C) 1997-2001 Shugo Maeda */
-/* body of line_buffer() from irb enhancements at http://www.creo.hu/~csaba/ruby/ */
-
-#ifdef HAVE_READLINE_READLINE_H
-#include "ruby.h"
-#include <errno.h>
-#include <stdio.h>
-#include <readline/readline.h>
-
-static VALUE line_buffer(VALUE self)
-{
- rb_secure(4);
- if (rl_line_buffer == NULL)
- return Qnil;
- return rb_tainted_str_new2(rl_line_buffer);
-}
-
-void Init_readline_line_buffer() {
- VALUE c = rb_cObject;
- c = rb_const_get(c, rb_intern("Readline"));
- rb_define_singleton_method(c, "line_buffer", (VALUE(*)(ANYARGS))line_buffer, -1);
-}
-#endif
View
32 lib/bond/readline.rb
@@ -1,3 +1,5 @@
+require 'readline'
+
module Bond
# This is the default readline plugin for Bond. A valid plugin must be a module that defines methods setup
# and line_buffer as described below.
@@ -6,9 +8,6 @@ module Readline
# Loads the readline-like library and sets the completion_proc to the given agent.
def setup(agent)
- require 'readline'
- load_extension unless ::Readline.respond_to?(:line_buffer)
-
# Reinforcing irb defaults
::Readline.completion_append_character = nil
if ::Readline.respond_to?("basic_word_break_characters=")
@@ -21,15 +20,24 @@ def setup(agent)
end
end
- def load_extension
- require 'readline_line_buffer'
- rescue LoadError
- $stderr.puts "Bond Error: Failed to load readline_line_buffer.bundle. Ensure that it exists and was built correctly."
- end
+ if ::Readline.respond_to?(:line_buffer)
+ # Returns full line of what the user has typed.
+ def line_buffer
+ ::Readline.line_buffer
+ end
+ else
+ require 'ffi'
+ extend FFI::Library
- # Returns full line of what the user has typed.
- def line_buffer
- ::Readline.line_buffer
+ ffi_lib 'readline'
+
+ attach_variable :rl_line_buffer, :pointer
+
+ def line_buffer
+ unless Readline.rl_line_buffer.null?
+ Readline.rl_line_buffer.get_string(0)
+ end
+ end
end
end
-end
+end
Something went wrong with that request. Please try again.