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 * Simple executor for running XTCL commands from command line, ¨
13 * i.e. console, std. input
14 *
15 * @author tomp
16 */
17 public class InteractiveExecutor {
18
19 /*** Context for commands compilation and execution */
20 protected Context context;
21
22 /***
23 * Creates a new instance of InteractiveExecutor
24 *
25 * @param c Context for the Executor
26 */
27 protected InteractiveExecutor(Context c) {
28 context = c;
29 }
30
31 /***
32 * Executes a command in the current context.
33 *
34 * Runs interactively in a loop until END command is entered
35 *
36 * @return DOCUMENT ME!
37 * @throws Exception DOCUMENT ME!
38 */
39 public int interactive() throws Exception {
40 CommandLineReader clr = new CommandLineReader();
41 Command cs = null;
42 int retCode = 0;
43 int commandNo = 0;
44 PrintStream out = context.getOut();
45 List history = new ArrayList();
46
47 do {
48 if (out != null) {
49 out.print("\nXTCL " + commandNo + ">");
50 }
51
52 String s = clr.nextCommandLine();
53
54
55 if (s == null) {
56 break;
57 }
58
59
60 if (Character.isDigit(s.charAt(0))) {
61
62 int pos = Integer.parseInt(s);
63
64 cs = (Command) history.get(pos);
65 retCode = context.execute(cs);
66
67
68 } else if (s.toLowerCase().startsWith("hi")) {
69
70 int c = 0;
71
72 for (Iterator i = history.iterator(); i.hasNext(); c++) {
73 cs = (Command) i.next();
74 System.out.println(c + "> " + cs);
75 }
76
77
78 } else {
79
80 Compiler comp = context.getCompiler();
81
82 cs = comp.compile(s, clr);
83 retCode = context.execute(cs);
84
85 if (cs != null) {
86 history.add(cs);
87 }
88 }
89
90 commandNo++;
91 } while (!(cs instanceof End));
92
93 return retCode;
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106