Index: src/org/jruby/Main.java
===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/Main.java,v
retrieving revision 1.35
diff -u -r1.35 Main.java
--- src/org/jruby/Main.java	24 Dec 2005 12:00:25 -0000	1.35
+++ src/org/jruby/Main.java	23 Feb 2006 21:03:49 -0000
@@ -89,7 +89,7 @@
         // do not want to exit on non-errors since the interpreter may have
         // started background threads (ala samples/swing2.rb)
         if (status != 0) {
-        	System.exit(status);
+        	//System.exit(status);
         }
     }
 
Index: src/org/jruby/RubyKernel.java
===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyKernel.java,v
retrieving revision 1.41
diff -u -r1.41 RubyKernel.java
--- src/org/jruby/RubyKernel.java	18 Feb 2006 15:38:12 -0000	1.41
+++ src/org/jruby/RubyKernel.java	23 Feb 2006 21:03:50 -0000
@@ -38,7 +38,11 @@
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
 
 import org.jruby.ast.util.ArgsUtil;
 import org.jruby.exceptions.JumpException;
@@ -643,48 +647,136 @@
         
         return recv.getRuntime().newString(output.toString());
     }
+    
+    private static String[] parseCommandLine(String command) {
+        List argsList = new ArrayList();
+        StringTokenizer st = new StringTokenizer(command, " ");
+        if (command.startsWith("ruby") | command.indexOf("jruby") != -1) {
+            st.nextToken();
+        }
+        char quoteChar = 0;
+        while (st.hasMoreTokens()) {
+            String token = st.nextToken();
+            
+            if (quoteChar == 0) {
+                argsList.add(token);
+                if (token.charAt(0) == '"') {
+                    quoteChar = '"';
+                } else if (token.charAt(0) == '\'') {
+                    quoteChar = '\'';
+                }
+                
+                if (token.charAt(token.length() - 1) == quoteChar) {
+                    String arg = (String)argsList.get(argsList.size() - 1);
+                    if (arg.startsWith("\"") || arg.startsWith("'")) {
+                        arg = arg.substring(1);
+                    }
+                    if (arg.endsWith("\"") || arg.startsWith("'")) {
+                        arg = arg.substring(0, arg.length() - 1);
+                    }
+                    argsList.set(argsList.size() - 1, arg);
+                    quoteChar = 0;
+                }
+            } else {
+                argsList.set(argsList.size() - 1, argsList.get(argsList.size() - 1) + " " + token);
+                if (token.charAt(token.length() - 1) == quoteChar) {
+                    String arg = (String)argsList.get(argsList.size() - 1);
+                    if (arg.startsWith("\"") || arg.startsWith("'")) {
+                        arg = arg.substring(1);
+                    }
+                    if (arg.endsWith("\"") || arg.startsWith("'")) {
+                        arg = arg.substring(0, arg.length() - 1);
+                    }
+                    argsList.set(argsList.size() - 1, arg);
+                    quoteChar = 0;
+                }
+            }
+        }
+        String[] args = new String[argsList.size()];
+        argsList.toArray(args);
+        
+        return args;
+    }
 
-    private static int runInShell(IRuby runtime, String command, StringBuffer output) {
+    private static int runInShell(IRuby runtime, final String command, StringBuffer output) {
         try {
             String shell = System.getProperty("jruby.shell");
-            Process aProcess;
+            Process aProcess = null;
             String shellSwitch = "-c";
+            System.out.println("command: " + command);
             if (shell != null) {
-                if (!shell.endsWith("sh")) {
-                    shellSwitch = "/c";
+                if (command.startsWith("ruby") || command.indexOf("jruby") != -1) {
+                    final String[] args = parseCommandLine(command);
+                    System.out.println(Arrays.asList(args));
+                    Thread t = new Thread() {
+                        public void run() {
+                            try {
+                                org.jruby.Main.main(args);
+                            } catch (Throwable t) {
+                            }
+                        }
+                    };
+                    t.start();
+                    t.join();
+                } else {
+                    if (!shell.endsWith("sh")) {
+                        shellSwitch = "/c";
+                    }
+                    aProcess = Runtime.getRuntime().exec(new String[] { shell, shellSwitch, command });
                 }
-                aProcess = Runtime.getRuntime().exec(new String[] { shell, shellSwitch, command });
             } else {
-                aProcess = Runtime.getRuntime().exec(command);
+                if (command.indexOf("jruby") != -1) {
+                    List argsList = new ArrayList();
+                    StringTokenizer st = new StringTokenizer(command, " ");
+                    st.nextToken();
+                    while (st.hasMoreTokens()) {
+                        argsList.add(st.nextToken());
+                    }
+                    final String[] args = new String[argsList.size()];
+                    argsList.toArray(args);
+                    Thread t = new Thread() {
+                        public void run() {
+                            org.jruby.Main.main(args);
+                        }
+                    };
+                    t.start();
+                    t.join();
+                } else {
+                    aProcess = Runtime.getRuntime().exec(command);
+                }
             }
 
-            final BufferedReader reader = new BufferedReader(new InputStreamReader(aProcess.getInputStream()));
-
-            // Fairly innefficient impl, but readLine is unable to tell 
-            // whether the last line in a process ended with a newline or not.
-            int c;
-            boolean crSeen = false;
-            while ((c = reader.read()) != -1) {
-            	if (c == '\r') {
-            		crSeen = true;
-            	} else {
-            		if (crSeen) {
-            			if (c != '\n') {
-            				output.append('\r');
-            			}
-            			crSeen = false;
-            		}
-            		output.append((char)c);
-            	}
-            }
-            if (crSeen) {
-            	output.append('\r');
-            }
-            aProcess.getErrorStream().close();
-            aProcess.getOutputStream().close();
-            reader.close();
-            
-            return aProcess.waitFor();
+            if (aProcess != null) {
+                final BufferedReader reader = new BufferedReader(new InputStreamReader(aProcess.getInputStream()));
+    
+                // Fairly innefficient impl, but readLine is unable to tell 
+                // whether the last line in a process ended with a newline or not.
+                int c;
+                boolean crSeen = false;
+                while ((c = reader.read()) != -1) {
+                	if (c == '\r') {
+                		crSeen = true;
+                	} else {
+                		if (crSeen) {
+                			if (c != '\n') {
+                				output.append('\r');
+                			}
+                			crSeen = false;
+                		}
+                		output.append((char)c);
+                	}
+                }
+                if (crSeen) {
+                	output.append('\r');
+                }
+                aProcess.getErrorStream().close();
+                aProcess.getOutputStream().close();
+                reader.close();
+                
+                return aProcess.waitFor();
+            } else {
+                return 0;
+            }
         } catch (IOException e) {
             throw runtime.newIOErrorFromException(e);
         } catch (InterruptedException e) {
