Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / debugger / CommandStream.java
1 package jde.debugger;
2 import java.io.StreamTokenizer;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.PipedInputStream;
8 import java.io.PipedOutputStream;
9 import java.io.PrintWriter;
10 import java.io.Reader;
11 import java.io.StreamTokenizer;
12 import java.util.ArrayList;
13 import java.util.List;
14
15
16
17 /**
18  * CommandStream.java
19  *
20  *
21  * Created: Tue Feb 13 15:40:34 2001
22  *
23  * @author <a href="mailto: "</a>
24  * @version 1.0
25  */
26
27 public class CommandStream extends StreamTokenizer implements Protocol {
28
29   public CommandStream (BufferedReader in){
30
31     super(in);
32       m_reader = in;
33     setSyntax();
34   }
35
36   public List nextCommand() {
37
38     List commandLine = new ArrayList();
39
40     try {
41
42       int token = nextToken();
43
44       while (token != TT_EOL) {
45
46         switch (token) {
47
48         case TT_EOF :
49           throw new IOException("EOF occurred reading command stream.");
50
51         case TT_WORD :
52         case '"' :
53         case '\'':
54           commandLine.add(sval);
55           break;
56
57         default:
58           commandLine.add(String.valueOf((char)token));
59           break;
60         } // end of switch ()
61
62         token = nextToken();
63       }
64
65       if (commandLine.size() < 3) {
66         if (commandLine.size() > 0) {
67           JDE.commandResult(new Integer(-1), "Malformed command: size=" + commandLine.size(),
68                             CMD_NOK, QUOTE);
69           JDE.debug(COMMANDS, commandLine.toString());
70         }
71         commandLine = nextCommand();
72       }
73     }
74     catch (IOException ex) {
75       commandLine = null;
76     } // end of catch
77
78     return commandLine;
79   }
80
81   /**
82    * Sets the syntax of the command stream. We want the input to be broken
83    * into lines, with whitespaces separating tokens
84    */
85   private void setSyntax() {
86     resetSyntax();
87     eolIsSignificant(true);
88     whitespaceChars('\u0000', '\u0020');
89     wordChars('\u0021', '\u00ff');
90     quoteChar('"');
91   }
92
93   public static void main (String[] args) {
94
95     PrintWriter out = new PrintWriter(System.out);
96     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
97     CommandStream commandStream = new CommandStream(in);
98
99     out.print(">  ");
100     out.flush();
101     List command = commandStream.nextCommand();
102     while (command != null && ! ((String) command.get(2)).startsWith("qu")) {
103
104       int n = command.size();
105       for (int i = 0; i<n; i++) {
106         out.println(command.get(i));
107       } // end of for ()
108
109       out.print("> ");
110       out.flush();
111       command = commandStream.nextCommand();
112     } // end of while ()
113
114
115   } // end of main ()
116
117   private final Reader m_reader;
118
119   // ----------------------------------------
120   // Fix for bug 4809647
121   // ----------------------------------------
122   static {
123       try {
124         StdIn.fix();
125       } catch (IOException exc) {
126         throw new ExceptionInInitializerError(exc);
127       }
128   };
129
130   /**
131    * You must call <code>StdIn.fix()</code> once before using
132    <code>System.in</code>.
133   */
134   private static final class StdIn
135   {
136     private static InputStream       in;
137     private static PipedInputStream  pis;
138     private static PipedOutputStream pos;
139
140     private static Thread            pump = new Thread("StdIn Pump")
141       {
142         public void run()
143         {
144
145           byte[] buf = new byte[5120];
146
147           try {
148             while (true) {
149               int available = in.available();
150
151               if (available == 0) {
152                 Thread.sleep(50);
153                 continue;
154               }
155
156               int howMany = Math.min(buf.length, available);
157
158
159               //This works because we asked how many are there
160               in.read(buf, 0, howMany);
161               pos.write(buf, 0, howMany);
162 //            JDE.debug(JDE_PIPE, "Pumped " + howMany + " bytes to stdin, " +
163 //                      "starting with '" +
164 //                      new String(buf, 0, Math.min(20, howMany)) + "...'");
165
166             }
167           }
168           catch (Exception ex) {
169             ex.printStackTrace();
170           }
171         }
172
173       };
174
175     /**
176      * Nobody can create an instance of this class
177      */
178     private StdIn() {}
179
180     /**
181      * This method replaces System.in and workarounds bug
182      #4809647
183     */
184     public synchronized static void fix()
185       throws IOException
186     {
187       if (in != null) {
188         return;
189       }
190
191       in  = System.in;
192       pos = new PipedOutputStream();
193       pis = new PipedInputStream(pos);
194       System.setIn(pis);
195       pump.setDaemon(true);
196       pump.setPriority(Thread.MIN_PRIORITY);
197       pump.start();
198     }
199   }
200   // ----------------------------------------
201   // End Fix for bug 4809647
202   // ----------------------------------------
203
204 }// CommandStream
205 /*
206  * $Log: CommandStream.java,v $
207  * Revision 1.5  2004/12/24 16:05:12  troy
208  * Add window to display threads and stacks
209  *
210  * Revision 1.4  2003/04/29 16:51:56  troy
211  * Initial version of GUI.  Includes display of local variables.
212  *
213  * Revision 1.3  2003/01/08 07:03:08  paulk
214  * Remove carriage returns.
215  *
216  * Revision 1.2  2003/01/08 06:53:38  paulk
217  * Integrate Petter Mahlen's updates.
218  *
219  */
220
221 // End of CommandStream.java
222
223