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