From 72e29b31ee4f47a368d7021d685259982aa408a4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@iki.fi>
Date: Thu, 25 Nov 2010 22:44:33 +0200
Subject: [PATCH 2/4] - Use jline in bsh script for command history support.

---
 bsh-readline.patch |  151 ----------------------------------------------------
 bsh.spec           |   22 ++++----
 2 files changed, 10 insertions(+), 163 deletions(-)
 delete mode 100644 bsh-readline.patch

diff --git a/bsh-readline.patch b/bsh-readline.patch
deleted file mode 100644
index 999c317..0000000
--- a/bsh-readline.patch
+++ /dev/null
@@ -1,151 +0,0 @@
---- BeanShell/src/bsh/Interpreter.java~	2003-09-03 19:56:58.000000000 -0400
-+++ BeanShell/src/bsh/Interpreter.java	2004-01-25 09:59:41.730059108 -0500
-@@ -38,6 +38,13 @@
- import java.lang.reflect.Method;
- import java.lang.reflect.InvocationTargetException;
- 
-+import bsh.util.BshCompleter;
-+import bsh.util.NameCompletionTable;
-+import bsh.classpath.ClassManagerImpl;
-+import org.gnu.readline.Readline;
-+import org.gnu.readline.ReadlineLibrary;
-+import org.gnu.readline.ReadlineReader;
-+
- /**
- 	The BeanShell script interpreter.
- 
-@@ -394,10 +401,59 @@
- 			else
- 				src = System.in;
- 
--            Reader in = new CommandLineReader( new InputStreamReader(src));
--            Interpreter interpreter = 
--				new Interpreter( in, System.out, System.err, true );
--        	interpreter.run();
-+            Reader in = null;
-+            boolean usingReadline = false;
-+            String backingLib = System.getProperty("bsh.console.readlinelib");
-+            if (backingLib != null) {
-+                try {
-+                    File history = new File(System.getProperty("user.home") +
-+                                            File.separator + ".bsh_history");
-+                    if (!history.exists()) {
-+                        try {
-+                            history.createNewFile();
-+                        } catch(IOException ioe) {
-+                            debug("Unable to create history  " + history.getAbsolutePath());
-+                        }
-+                    }
-+                    ReadlineLibrary lib = ReadlineLibrary.byName(backingLib);
-+                    // should I wrap CommandLineReader around it?
-+                    if (history.canWrite() && history.canRead()) {
-+                        in = new ReadlineReader("bsh % ", history,lib);
-+                    } else {
-+                        in = new ReadlineReader("bsh % ",lib);
-+                        debug("Unable to read/write history  " + history.getAbsolutePath());
-+                    }
-+                } catch (IOException ioe) {
-+                    System.err.println("Unable to invoke ReadlineReader " +
-+                                       "due to: " + ioe);
-+                }
-+            }
-+            if (in == null)
-+                in = new CommandLineReader( new InputStreamReader(src));
-+            else
-+                usingReadline = true;
-+            Interpreter interpreter =
-+                new Interpreter( in, System.out, System.err, true );
-+            if (usingReadline) {
-+                NameCompletionTable nct = new NameCompletionTable();
-+                nct.add(interpreter.getNameSpace());
-+
-+                /** ClassManager does a lot of chatting to the stdout,
-+                 *  so this has been commented out for the time being
-+                 **/
-+
-+//              try {
-+//                  BshClassManager bcm = BshClassManager.getClassManager();
-+//                      if (bcm != null) {
-+//                          nct.add(((ClassManagerImpl)bcm).getClassPath());
-+//                      }
-+//                  } catch(ClassPathException cpe) {
-+//                      debug("classpath exception in name compl:" + cpe);
-+//                  }
-+
-+                Readline.setCompleter(new BshCompleter(nct));
-+            }
-+            interpreter.run();
-         }
-     }
- 
-@@ -445,7 +501,7 @@
-                 System.err.flush();
-                 Thread.yield();  // this helps a little
- 
--                if ( interactive )
-+                if ( interactive && !(in instanceof ReadlineReader))
-                     print( getBshPrompt() );
- 
-                 eof = Line();
-@@ -548,10 +604,17 @@
-             }
-         }
- 
--		if ( interactive && exitOnEOF )
--			System.exit(0);
-+        if ( interactive && exitOnEOF ) {
-+            /* should be done for all streams in general, but this
-+             * ensures that the history for readline is flushed */
-+            try {
-+                in.close();
-+            } catch (IOException ioe) {
-+            }
-+	
-+            System.exit(0);
-     }
--
-+   }
- 	// begin source and eval
- 
- 	/**
---- /dev/null	2003-10-19 02:52:03.000000000 -0400
-+++ BeanShell/src/bsh/util/BshCompleter.java	2004-01-25 10:14:10.184458217 -0500
-@@ -0,0 +1,38 @@
-+package bsh.util;
-+
-+import org.gnu.readline.ReadlineCompleter;
-+
-+/**
-+ * An adapter for org.gnu.readline's ReadlineCompleter interface to map to
-+ * BeanShell's NameCompleter interface.
-+ *
-+ * @see org.gnu.readline.ReadlineReader
-+ * @version $Revision: 1.1 $
-+ * @author Shane Celis <shane@terraspring.com>
-+ **/
-+public class BshCompleter implements ReadlineCompleter {
-+
-+    private NameCompletion completer;
-+
-+    /**
-+     * Constructs a <code>ReadlineCompleter</code> out of a
-+     * <code>NameCompleter</code> object.
-+     **/
-+    public BshCompleter(NameCompletion completer) {
-+        this.completer = completer;
-+    }
-+
-+    /**
-+     * Returns String of completion if unambiguous, otherwise null
-+     **/
-+    public String completer(String text, int state) {
-+        // Not sure what state is used for in ReadlineCompleter
-+        String[] completions = completer.completeName(text);
-+        if (completions.length == 1 && state == 0) {
-+            return completions[0];
-+        } else {
-+            return null;        // ambiguous result
-+        }
-+    }
-+
-+}
diff --git a/bsh.spec b/bsh.spec
index 8df763b..51fcc23 100644
--- a/bsh.spec
+++ b/bsh.spec
@@ -43,12 +43,10 @@ Source2:        bsh-bsf-1.3.0.pom
 Source3:        %{name}-desktop.desktop
 
 Patch0:         %{name}-build.patch
-#Patch1:         %{name}-readline.patch
 BuildRequires:  ant, bsf, ant-trax, ImageMagick, desktop-file-utils
 BuildRequires:  servlet
 Requires:       bsf
 Requires:       jpackage-utils >= 0:1.7.2
-#BuildRequires:  libreadline-java
 URL:            http://www.beanshell.org/
 Group:          Development/Libraries
 BuildArch:      noarch
@@ -101,6 +99,7 @@ Demonstrations and samples for %{name}.
 Summary:        %{name} utilities
 Group:          Development/Tools
 Requires:       %{name} = %{epoch}:%{version}-%{release}
+Requires:       jline
 Provides:       %{name}-desktop = %{epoch}:%{version}-%{release}
 Obsoletes:      %{name}-desktop < 0:1.3.0-16
 # So that yum will pull this in on upgrades from < 0:1.3.0-16:
@@ -112,7 +111,6 @@ Obsoletes:      %{name} < 0:1.3.0-16
 %prep
 %setup -q -n BeanShell
 %patch0 -p1
-#%patch1 -p1
 for j in $(find . -name "*.jar"); do
     mv $j $j.no
 done
@@ -219,6 +217,11 @@ install -m 644 dist/bshservlet-wbsh.war $RPM_BUILD_ROOT%{_datadir}/%{name}/webap
 install -d $RPM_BUILD_ROOT%{_bindir}
 
 function bsh_script() {
+    local jars=%{name}.jar runclass=
+    if [ $2 = jline.ConsoleRunner ] ; then
+        jars="$jars jline.jar"
+        runclass=bsh.Interpreter
+    fi
 cat > $RPM_BUILD_ROOT%{_bindir}/$1 << EOF
 #!/bin/sh
 #
@@ -244,13 +247,7 @@ if [ -n "\$BSH_DEBUG" ]; then
   BASE_FLAGS=-Ddebug=true
 fi
 
-BASE_JARS="%{name}.jar"
-
-#if [ -f /usr/lib/libJavaReadline.so ]; then
-#  BASE_FLAGS="$BASE_FLAGS -Djava.library.path=/usr/lib"
-#  BASE_FLAGS="\$BASE_FLAGS -Dbsh.console.readlinelib=GnuReadline"
-#  BASE_JARS="\$BASE_JARS libreadline-java.jar"
-#fi
+BASE_JARS="$jars"
 
 # Set parameters
 set_jvm
@@ -259,11 +256,11 @@ set_flags \$BASE_FLAGS
 set_options \$BASE_OPTIONS
 
 # Let's start
-run "\$@"
+run $runclass "\$@"
 EOF
 }
 
-bsh_script bsh bsh.Interpreter
+bsh_script bsh jline.ConsoleRunner
 bsh_script bsh-desktop bsh.Console
 
 cat > $RPM_BUILD_ROOT%{_bindir}/%{name}doc << EOF
@@ -327,6 +324,7 @@ gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :
 * Thu Nov 25 2010 Ville Skyttä <ville.skytta@iki.fi> - 0:1.3.0-16
 - Rename -desktop to -utils, move shell scripts and menu entry to it (#417491).
 - Bring icon cache scriptlets up to date with current guidelines.
+- Use jline in bsh script for command history support.
 
 * Sat Jan 9 2010 Alexander Kurtakov <akurtako@redhat.com> 0:1.3.0-15.2
 - Drop gcj_support.
-- 
1.7.2.3

