Rev 1679 | Rev 2083 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1679 | Rev 1681 | ||
|---|---|---|---|
| 1 | - | ||
| 2 | Require |
- | |
| 3 | 1 | ||
| 4 | Console_Functions = {}; |
2 | Console_Functions = {}; |
| 5 | Console_CurrentFunction = null; |
3 | Console_CurrentFunction = null; |
| 6 | Console_DefaultPrompt = "\033[29;1m[atomic] \033[0m"; |
4 | Console_DefaultPrompt = "\033[29;1m[atomic] \033[0m"; |
| 7 | Console_LastClientId = null; |
5 | Console_LastClientId = null; |
| 8 | 6 | ||
| 9 | function Console_GetFunctionNames() |
7 | function Console_GetFunctionNames() |
| 10 | { |
8 | { |
| 11 | return get_keys(Console_Functions); |
9 | return get_keys(Console_Functions); |
| 12 | } |
10 | } |
| 13 | 11 | ||
| 14 | // C++ callable functions |
12 | // C++ callable functions |
| 15 | 13 | ||
| 16 | function Console_AutocompleteRequest(client_id, arg_index, commandline) |
14 | function Console_AutocompleteRequest(client_id, arg_index, commandline) |
| 17 | { |
15 | { |
| 18 | Console_LastClientId = client_id; |
16 | Console_LastClientId = client_id; |
| 19 | 17 | ||
| 20 | var result = []; |
18 | var result = []; |
| 21 | var parts = []; |
19 | var parts = []; |
| 22 | var name = "Console_Shell"; |
20 | var name = "Console_Shell"; |
| 23 | 21 | ||
| 24 | if (commandline.length > 0 && arg_index != 0) |
22 | if (commandline.length > 0 && arg_index != 0) |
| 25 | { |
23 | { |
| 26 | parts = commandline.split(" "); |
24 | parts = commandline.split(" "); |
| 27 | name = parts.shift(); |
25 | name = parts.shift(); |
| 28 | arg_index--; |
26 | arg_index--; |
| 29 | } |
27 | } |
| 30 | 28 | ||
| 31 | if (Console_Functions[name] && Console_Functions[name]["autocomplete"]) |
29 | if (Console_Functions[name] && Console_Functions[name]["autocomplete"]) |
| 32 | { |
30 | { |
| 33 | //Log("calling autocomplete_callback: " + name + "\n"); |
31 | //Log("calling autocomplete_callback: " + name + "\n"); |
| 34 | result = Console_Functions[name]["autocomplete"](arg_index, parts); |
32 | result = Console_Functions[name]["autocomplete"](arg_index, parts); |
| 35 | } |
33 | } |
| 36 | 34 | ||
| 37 | var result_string = result.join("\n"); |
35 | var result_string = result.join("\n"); |
| 38 | 36 | ||
| 39 | ConsoleExport_AutoCompleteResponse(Console_LastClientId, result_string); |
37 | ConsoleExport_AutoCompleteResponse(Console_LastClientId, result_string); |
| 40 | return true; |
38 | return true; |
| 41 | } |
39 | } |
| 42 | 40 | ||
| 43 | function Console_PromptResponse(client_id, response) |
41 | function Console_PromptResponse(client_id, response) |
| 44 | { |
42 | { |
| 45 | Console_LastClientId = client_id; |
43 | Console_LastClientId = client_id; |
| 46 | 44 | ||
| 47 | var parts = array_remove_empty(response.split(" ")); |
45 | var parts = array_remove_empty(response.split(" ")); |
| 48 | 46 | ||
| 49 | var current_function = Console_CurrentFunction; |
47 | var current_function = Console_CurrentFunction; |
| 50 | Console_CurrentFunction = Console_Shell; |
48 | Console_CurrentFunction = Console_Shell; |
| 51 | 49 | ||
| 52 | var result = current_function.apply(null, Array.prototype.slice.call(parts, 0)); |
50 | var result = current_function.apply(null, Array.prototype.slice.call(parts, 0)); |
| 53 | 51 | ||
| 54 | if (Console_CurrentFunction == Console_Shell) |
52 | if (Console_CurrentFunction == Console_Shell) |
| 55 | { |
53 | { |
| 56 | Console_SetDefaultPrompt(); |
54 | Console_SetDefaultPrompt(); |
| 57 | } |
55 | } |
| 58 | 56 | ||
| 59 | return result; |
57 | return result; |
| 60 | } |
58 | } |
| 61 | 59 | ||
| 62 | function Console_NewConnection(client_id) |
60 | function Console_NewConnection(client_id) |
| 63 | { |
61 | { |
| 64 | Console_LastClientId = client_id; |
62 | Console_LastClientId = client_id; |
| 65 | 63 | ||
| 66 | return Console_SetDefaultPrompt(); |
64 | return Console_SetDefaultPrompt(); |
| 67 | } |
65 | } |
| 68 | 66 | ||
| 69 | // User script functions |
67 | // User script functions |
| 70 | 68 | ||
| 71 | function Console_GetClientId() |
69 | function Console_GetClientId() |
| 72 | { |
70 | { |
| 73 | return Console_LastClientId; |
71 | return Console_LastClientId; |
| 74 | } |
72 | } |
| 75 | 73 | ||
| 76 | function Console_LogToClient(client_id, text) |
74 | function Console_LogToClient(client_id, text) |
| 77 | { |
75 | { |
| 78 | if (client_id) |
76 | if (client_id) |
| 79 | { |
77 | { |
| 80 | return ConsoleExport_LogToClient(client_id, text); |
78 | return ConsoleExport_LogToClient(client_id, text); |
| 81 | } |
79 | } |
| 82 | 80 | ||
| 83 | return false; |
81 | return false; |
| 84 | } |
82 | } |
| 85 | 83 | ||
| 86 | function Console_PreventDefaultPrompt() |
84 | function Console_PreventDefaultPrompt() |
| 87 | { |
85 | { |
| 88 | Console_CurrentFunction = null; |
86 | Console_CurrentFunction = null; |
| 89 | } |
87 | } |
| 90 | 88 | ||
| 91 | function Console_SetDefaultPrompt() |
89 | function Console_SetDefaultPrompt() |
| 92 | { |
90 | { |
| 93 | return Console_PromptRequest(Console_DefaultPrompt, Console_Shell); |
91 | return Console_PromptRequest(Console_DefaultPrompt, Console_Shell); |
| 94 | } |
92 | } |
| 95 | 93 | ||
| 96 | function Console_PromptRequest(prompt, callback) |
94 | function Console_PromptRequest(prompt, callback) |
| 97 | { |
95 | { |
| 98 | Console_CurrentFunction = callback; |
96 | Console_CurrentFunction = callback; |
| 99 | return ConsoleExport_PromptRequest(Console_LastClientId, prompt); |
97 | return ConsoleExport_PromptRequest(Console_LastClientId, prompt); |
| 100 | } |
98 | } |
| 101 | 99 | ||
| 102 | function Console_RegisterCommand(command, autocomplete_callback) |
100 | function Console_RegisterCommand(command, autocomplete_callback) |
| 103 | { |
101 | { |
| 104 | var command_string = command.toString(); |
102 | var command_string = command.toString(); |
| 105 | 103 | ||
| 106 | var pos_end = command_string.indexOf("\n") - 1; |
104 | var pos_end = command_string.indexOf("\n") - 1; |
| 107 | var pos_start = command_string.indexOf(" ") + 1; |
105 | var pos_start = command_string.indexOf(" ") + 1; |
| 108 | 106 | ||
| 109 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
107 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
| 110 | 108 | ||
| 111 | var pos_split = full_name.indexOf("("); |
109 | var pos_split = full_name.indexOf("("); |
| 112 | 110 | ||
| 113 | var name = full_name.substr(0, pos_split); |
111 | var name = full_name.substr(0, pos_split); |
| 114 | 112 | ||
| 115 | var argument_string = full_name.substr(pos_split + 1); |
113 | var argument_string = full_name.substr(pos_split + 1); |
| 116 | 114 | ||
| 117 | if (argument_string.length > 0) |
115 | if (argument_string.length > 0) |
| 118 | { |
116 | { |
| 119 | argument_string = argument_string.replace(/,/g, ""); |
117 | argument_string = argument_string.replace(/,/g, ""); |
| 120 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
118 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
| 121 | } |
119 | } |
| 122 | 120 | ||
| 123 | Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string }; |
121 | Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string }; |
| 124 | return true; |
122 | return true; |
| 125 | } |
123 | } |
| 126 | 124 | ||
| 127 | function Console_StandardAutocomplete() |
125 | function Console_StandardAutocomplete() |
| 128 | { |
126 | { |
| 129 | var arg_index = arguments[0]; |
127 | var arg_index = arguments[0]; |
| 130 | var args = arguments[1]; |
128 | var args = arguments[1]; |
| 131 | 129 | ||
| 132 | if (0 <= arg_index && arg_index < arguments.length - 2) |
130 | if (0 <= arg_index && arg_index < arguments.length - 2) |
| 133 | { |
131 | { |
| 134 | return arguments[arg_index + 2]; |
132 | return arguments[arg_index + 2]; |
| 135 | } |
133 | } |
| 136 | 134 | ||
| 137 | return []; |
135 | return []; |
| 138 | } |
136 | } |
| 139 | 137 | ||
| 140 | // Default call function |
138 | // Default call function |
| 141 | 139 | ||
| 142 | function Console_Shell() |
140 | function Console_Shell() |
| 143 | { |
141 | { |
| 144 | var args = to_array(arguments); |
142 | var args = to_array(arguments); |
| 145 | var name = args.shift(); |
143 | var name = args.shift(); |
| 146 | 144 | ||
| 147 | if (Console_Functions[name] && Console_Functions[name]["command"]) |
145 | if (Console_Functions[name] && Console_Functions[name]["command"]) |
| 148 | { |
146 | { |
| 149 | return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0)); |
147 | return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0)); |
| 150 | } |
148 | } |
| 151 | else |
149 | else |
| 152 | { |
150 | { |
| 153 | Log("\033[31mNo such command found, " + name + ".\033[0m\n"); |
151 | Log("\033[31mNo such command found, " + name + ".\033[0m\n"); |
| 154 | } |
152 | } |
| 155 | 153 | ||
| 156 | return false; |
154 | return false; |
| 157 | } |
155 | } |
| 158 | Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
156 | Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
| 159 | 157 | ||
| 160 | // Help functions |
158 | // Help functions |
| 161 | 159 | ||
| 162 | function help_complete(arg_index, args) |
160 | function help_complete(arg_index, args) |
| 163 | { |
161 | { |
| 164 | var result = []; |
162 | var result = []; |
| 165 | 163 | ||
| 166 | if (arg_index == 1) // command name |
164 | if (arg_index == 1) // command name |
| 167 | { |
165 | { |
| 168 | for (var name in Console_Functions) |
166 | for (var name in Console_Functions) |
| 169 | { |
167 | { |
| 170 | result.push(name); |
168 | result.push(name); |
| 171 | } |
169 | } |
| 172 | } |
170 | } |
| 173 | 171 | ||
| 174 | return result; |
172 | return result; |
| 175 | } |
173 | } |
| 176 | 174 | ||
| 177 | function help(command_name) |
175 | function help(command_name) |
| 178 | { |
176 | { |
| 179 | if (!command_name) |
177 | if (!command_name) |
| 180 | { |
178 | { |
| 181 | command_name = "help"; |
179 | command_name = "help"; |
| 182 | } |
180 | } |
| 183 | 181 | ||
| 184 | if (Console_Functions[command_name] && Console_Functions[command_name]["help"]) |
182 | if (Console_Functions[command_name] && Console_Functions[command_name]["help"]) |
| 185 | { |
183 | { |
| 186 | var result = ""; |
184 | var result = ""; |
| 187 | 185 | ||
| 188 | if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"]) |
186 | if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"]) |
| 189 | { |
187 | { |
| 190 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
188 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
| 191 | } |
189 | } |
| 192 | 190 | ||
| 193 | result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n"; |
191 | result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n"; |
| 194 | 192 | ||
| 195 | Log(result); |
193 | Log(result); |
| 196 | return true; |
194 | return true; |
| 197 | } |
195 | } |
| 198 | 196 | ||
| 199 | Log("\033[31mNo help for " + command_name + " found.\033[0m\n"); |
197 | Log("\033[31mNo help for " + command_name + " found.\033[0m\n"); |
| 200 | return false; |
198 | return false; |
| 201 | } |
199 | } |
| 202 | Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
200 | Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
| 203 | 201 | ||
| 204 | function version() |
202 | function version() |
| 205 | { |
203 | { |
| 206 | Log(SystemExport_Version() + "\n"); |
204 | Log(SystemExport_Version() + "\n"); |
| 207 | return true; |
205 | return true; |
| 208 | } |
206 | } |
| 209 | Console_RegisterCommand(version); |
207 | Console_RegisterCommand(version); |
| 210 | 208 | ||
| 211 | function license() |
209 | function license() |
| 212 | { |
210 | { |
| 213 | Log(SystemExport_License() + "\n"); |
211 | Log(SystemExport_License() + "\n"); |
| 214 | return true; |
212 | return true; |
| 215 | } |
213 | } |
| 216 | Console_RegisterCommand(license); |
214 | Console_RegisterCommand(license); |
| 217 | 215 | ||
| 218 | function quit() |
216 | function quit() |
| 219 | { |
217 | { |
| 220 | Log("Goodbye!\n"); |
218 | Log("Goodbye!\n"); |
| 221 | 219 | ||
| 222 | ConsoleExport_DisconnectClient(Console_LastClientId); |
220 | ConsoleExport_DisconnectClient(Console_LastClientId); |
| 223 | Console_LastClientId = null; |
221 | Console_LastClientId = null; |
| 224 | return false; |
222 | return false; |
| 225 | } |
223 | } |
| 226 | Console_RegisterCommand(quit); |
224 | Console_RegisterCommand(quit); |
| 227 | 225 | ||