1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.cmdl;
21
22 import org.apache.log4j.Logger;
23 import org.xnap.XNap;
24 import org.xnap.plugin.*;
25
26 public class CommandLine implements Runnable {
27
28
29
30 private static Logger logger = Logger.getLogger(CommandLine.class);
31 private static CommandLine singleton = null;
32
33 private ReadlineConsole console = new ReadlineConsole();
34
35
36
37 public CommandLine()
38 {
39 }
40
41
42
43 /***
44 * Returns the instance of CommandLine.
45 *
46 * @return null, if the command line was not started
47 */
48 public static CommandLine getInstance()
49 {
50 return singleton;
51 }
52
53 public void run()
54 {
55 init();
56
57 while (true) {
58 String cmd = console.readln("XNap> ");
59 if (cmd == null) {
60 break;
61 }
62
63 try {
64 if (!Executer.parse(cmd, console)) {
65 console.println("unknown command (h for help)");
66 }
67 }
68 catch (Exception e) {
69 logger.debug("exception during execution: " + cmd, e);
70 console.println("error: " + e.getLocalizedMessage());
71 }
72 }
73 }
74
75 /***
76 * Starts the command line.
77 */
78 public void init()
79 {
80 Executer.add(new ExitCmd());
81
82 String msg
83 = "XNap " + PluginManager.getCoreVersion()
84 + " (http://xnap.sourceforge.net).\n\n"
85 + "XNap comes with ABSOLUTELY NO WARRANTY; for details "
86 + "type 'readme'.\n"
87 + "This is free software, and you are welcome to redistribute it\n"
88 + "under certain conditions; type 'license' for details.\n\n"
89 + "Ready (h for help, q to exit)\n";
90 console.println(msg);
91 }
92
93 public void start()
94 {
95 Thread t = new Thread(this, "CommandLine");
96 t.start();
97 }
98
99 public void stop()
100 {
101 console.saveSettings();
102 }
103
104 private class ExitCmd extends AbstractCommand
105 {
106 public ExitCmd()
107 {
108 putValue(CMD, "exit");
109 putValue(ALIASES, new String[] { "quit", "q" });
110 putValue(SHORT_HELP, "Quits the application.");
111 }
112
113 public void execute(String command, Console console)
114 {
115 XNap.exit();
116 }
117 }
118
119 }