1 package net.sf.tomp.xtcl;
2
3 import net.sf.tomp.xtcl.command.End;
4
5 import java.io.PrintStream;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10
11 /***
12 * DOCUMENT ME!
13 *
14 * @author tomp
15 */
16 public class InteractiveExecutor {
17 /*** DOCUMENT ME! */
18 protected Context context;
19
20 /***
21 * Creates a new instance of InteractiveExecutor
22 *
23 * @param c Context for the Executor
24 */
25 protected InteractiveExecutor(Context c) {
26 context = c;
27 }
28
29 /***
30 * Executes a command in the current context
31 *
32 * @return DOCUMENT ME!
33 * @throws Exception DOCUMENT ME!
34 */
35 public int interactive() throws Exception {
36 CommandLineReader clr = new CommandLineReader();
37 Command cs = null;
38 int retCode = 0;
39 int commandNo = 0;
40 PrintStream out = context.getOut();
41 List history = new ArrayList();
42
43 do {
44 if (out != null) {
45 out.print("\nXTCL " + commandNo + ">");
46 }
47
48 String s = clr.nextCommandLine();
49
50
51 if (s == null) {
52 break;
53 }
54
55
56 if (Character.isDigit(s.charAt(0))) {
57
58 int pos = Integer.parseInt(s);
59
60 cs = (Command) history.get(pos);
61 retCode = context.execute(cs);
62
63
64 } else if (s.toLowerCase().startsWith("hi")) {
65
66 int c = 0;
67
68 for (Iterator i = history.iterator(); i.hasNext(); c++) {
69 cs = (Command) i.next();
70 System.out.println(c + "> " + cs);
71 }
72
73
74 } else {
75
76 Compiler comp = context.getCompiler();
77
78 cs = comp.compile(s, clr);
79 retCode = context.execute(cs);
80
81 if (cs != null) {
82 history.add(cs);
83 }
84 }
85
86 commandNo++;
87 } while (!(cs instanceof End));
88
89 return retCode;
90 }
91 }
92
93
94
95
96
97
98
99
100
101
102