1 21 22 package org.armedbear.j; 23 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.net.ServerSocket ; 27 import java.net.Socket ; 28 import javax.swing.SwingUtilities ; 29 import org.armedbear.lisp.ConditionThrowable; 30 import org.armedbear.lisp.Interpreter; 31 import org.armedbear.lisp.Lisp; 32 import org.armedbear.lisp.LispObject; 33 34 public final class JLisp extends LispShell 35 { 36 private Thread thread; 37 private final File initialDir; 38 39 private int port; 40 private Socket socket; 41 private Interpreter interpreter; 42 43 private JLisp(File initialDir) 44 { 45 super(); 46 this.initialDir = initialDir; 47 title = "jlisp"; 48 mode = LispShellMode.getMode(); 49 formatter = mode.getFormatter(this); 50 setInitialized(true); 51 } 52 53 protected void initializeHistory() 54 { 55 history = new History("jlisp.history", 30); 56 } 57 58 public String toString() 59 { 60 return "jlisp"; 61 } 62 63 protected boolean checkProcess() 65 { 66 return true; 67 } 68 69 protected void startProcess() 70 { 71 thread = new Thread ("JLisp interpreter") { 72 public void run() 73 { 74 try { 75 startServer(); 76 if (interpreter != null) { 77 Editor.setLispInitialized(true); 78 interpreter.run(null); 79 } 80 } 81 catch (Exception e) { 82 Log.error(e); 83 } 84 Log.debug("interpreter thread exiting"); 85 Lisp.resetIO(); 86 Runnable processExitedRunnable = new Runnable () { 87 public void run() 88 { 89 appendString("\nProcess exited\n"); 90 updateDisplayInAllFrames(); 91 } 92 }; 93 SwingUtilities.invokeLater(processExitedRunnable); 94 } 95 }; 96 thread.setDaemon(true); 97 thread.start(); 98 99 synchronized (this) { 100 if (port == 0) { 101 try { 102 wait(3000); 103 } 104 catch (InterruptedException e) { 105 Log.error(e); 106 } 107 if (port == 0) 108 return; 109 } 110 } 111 112 try { 113 socket = new Socket ("localhost", port); 114 stdin = new OutputStreamWriter (socket.getOutputStream()); 115 stdoutThread = new StdoutThread(socket.getInputStream()); 116 stdoutThread.setName("JLisp reader"); 117 stdoutThread.setDaemon(true); 118 stdoutThread.start(); 119 } 120 catch (IOException e) { 121 Log.error(e); 122 } 123 } 124 125 private void startServer() 126 { 127 try { 128 ServerSocket serverSocket = new ServerSocket (0); 129 port = serverSocket.getLocalPort(); 130 synchronized (this) { 131 notify(); 132 } 133 Socket socket = serverSocket.accept(); interpreter = 135 Interpreter.createInstance(socket.getInputStream(), 136 socket.getOutputStream(), 137 initialDir.canonicalPath()); 138 if (interpreter != null) { 139 interpreter.getStandardOutput()._writeLine( 141 Version.getLongVersionString()); 142 } 143 } 144 catch (Throwable t) { 145 Log.error(t); 146 } 147 } 148 149 public synchronized void dispose() 150 { 151 Thread disposeThread = new Thread ("JLisp dispose") { 152 public void run() 153 { 154 Log.debug("JLisp.dispose"); 155 if (interpreter != null) 156 interpreter.kill(); 157 if (socket != null) { 158 try { 159 Log.debug("closing socket"); 160 socket.close(); 161 Log.debug("back from closing socket"); 162 } 163 catch (IOException e) { 164 Log.debug(e); 165 } 166 socket = null; 167 } 168 if (interpreter != null) { 169 interpreter.dispose(); 170 interpreter = null; 171 } 172 } 173 }; 174 disposeThread.setDaemon(true); 175 disposeThread.start(); 176 } 177 178 public static void jlisp() 179 { 180 final Editor editor = Editor.currentEditor(); 181 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 183 Buffer buf = it.nextBuffer(); 184 if (buf instanceof JLisp) { 185 editor.makeNext(buf); 186 editor.activateInOtherWindow(buf); 187 return; 188 } 189 } 190 editor.setWaitCursor(); 192 File initialDir = editor.getCurrentDirectory(); 193 if (initialDir == null || initialDir.isRemote()) 194 initialDir = Directories.getUserHomeDirectory(); 195 JLisp jlisp = new JLisp(initialDir); 196 jlisp.startProcess(); 197 editor.makeNext(jlisp); 198 Editor ed; 199 Buffer b = editor.getBuffer(); 200 if (b != null && b.isPaired()) { 201 Frame frame = editor.getFrame(); 202 editor.switchToBuffer(jlisp); 203 ed = frame.getCurrentEditor(); 204 } else 205 ed = editor.activateInOtherWindow(jlisp); 206 ed.eob(); 207 editor.setDefaultCursor(); 208 } 209 210 public static void runStartupScript(File file) throws ConditionThrowable 211 { 212 if (!Editor.isLispInitialized()) { 213 Interpreter.initializeLisp(true); 214 Editor.setLispInitialized(true); 215 } 216 FastStringBuffer sb = new FastStringBuffer("(load \""); 217 if (Platform.isPlatformWindows()) { 218 String cp = file.canonicalPath(); 219 final int limit = cp.length(); 220 for (int i = 0; i < limit; i++) { 221 char c = cp.charAt(i); 222 if (c == '\\') 223 sb.append('\\'); sb.append(c); 225 } 226 } else 227 sb.append(file.canonicalPath()); 228 sb.append("\")"); 229 Interpreter.evaluate(sb.toString()); 230 } 231 232 public static LispObject runLispCommand(String command) 233 throws ConditionThrowable 234 { 235 if (!Editor.isLispInitialized()) { 236 Interpreter.initializeLisp(true); 237 Editor.setLispInitialized(true); 238 } 239 return Interpreter.evaluate(command); 240 } 241 } 242 | Popular Tags |