KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > JLisp


1 /*
2  * JLisp.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: JLisp.java,v 1.22 2004/09/12 01:48:24 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.net.Socket JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
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 JavaDoc thread;
37     private final File initialDir;
38
39     private int port;
40     private Socket JavaDoc 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 JavaDoc toString()
59     {
60         return "jlisp";
61     }
62
63     // Returns true if underlying process is alive and well.
64
protected boolean checkProcess()
65     {
66         return true;
67     }
68
69     protected void startProcess()
70     {
71         thread = new Thread JavaDoc("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 JavaDoc e) {
82                     Log.error(e);
83                 }
84                 Log.debug("interpreter thread exiting");
85                 Lisp.resetIO();
86                 Runnable JavaDoc processExitedRunnable = new Runnable JavaDoc() {
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 JavaDoc e) {
105                     Log.error(e);
106                 }
107                 if (port == 0)
108                     return;
109             }
110         }
111
112         try {
113             socket = new Socket JavaDoc("localhost", port);
114             stdin = new OutputStreamWriter JavaDoc(socket.getOutputStream());
115             stdoutThread = new StdoutThread(socket.getInputStream());
116             stdoutThread.setName("JLisp reader");
117             stdoutThread.setDaemon(true);
118             stdoutThread.start();
119         }
120         catch (IOException JavaDoc e) {
121             Log.error(e);
122         }
123     }
124
125     private void startServer()
126     {
127         try {
128             ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(0);
129             port = serverSocket.getLocalPort();
130             synchronized (this) {
131                 notify();
132             }
133             Socket JavaDoc socket = serverSocket.accept(); // Blocks.
134
interpreter =
135                 Interpreter.createInstance(socket.getInputStream(),
136                                            socket.getOutputStream(),
137                                            initialDir.canonicalPath());
138             if (interpreter != null) {
139                 // Print j version banner.
140
interpreter.getStandardOutput()._writeLine(
141                     Version.getLongVersionString());
142             }
143         }
144         catch (Throwable JavaDoc t) {
145             Log.error(t);
146         }
147     }
148
149     public synchronized void dispose()
150     {
151         Thread JavaDoc disposeThread = new Thread JavaDoc("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 JavaDoc 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         // Look for existing jlisp buffer.
182
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         // Not found.
191
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 JavaDoc 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('\\'); // Double backslash.
224
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 JavaDoc 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