Rev 1647 | Rev 1657 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1647 | Rev 1649 | ||
|---|---|---|---|
| 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 = response.split(" "); |
45 | var parts = 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_PreventDefaultPrompt() |
69 | function Console_PreventDefaultPrompt() |
| 70 | { |
70 | { |
| 71 | Console_CurrentFunction = null; |
71 | Console_CurrentFunction = null; |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | function Console_SetDefaultPrompt() |
74 | function Console_SetDefaultPrompt() |
| 75 | { |
75 | { |
| 76 | return Console_PromptRequest(Console_DefaultPrompt, Console_Shell); |
76 | return Console_PromptRequest(Console_DefaultPrompt, Console_Shell); |
| 77 | } |
77 | } |
| 78 | 78 | ||
| 79 | function Console_PromptRequest(prompt, callback) |
79 | function Console_PromptRequest(prompt, callback) |
| 80 | { |
80 | { |
| 81 | Console_CurrentFunction = callback; |
81 | Console_CurrentFunction = callback; |
| 82 | return ConsoleExport_PromptRequest(Console_LastClientId, prompt); |
82 | return ConsoleExport_PromptRequest(Console_LastClientId, prompt); |
| 83 | } |
83 | } |
| 84 | 84 | ||
| 85 | function Console_RegisterCommand(command, autocomplete_callback) |
85 | function Console_RegisterCommand(command, autocomplete_callback) |
| 86 | { |
86 | { |
| 87 | var command_string = command.toString(); |
87 | var command_string = command.toString(); |
| 88 | 88 | ||
| 89 | var pos_end = command_string.indexOf("\n") - 1; |
89 | var pos_end = command_string.indexOf("\n") - 1; |
| 90 | var pos_start = command_string.indexOf(" ") + 1; |
90 | var pos_start = command_string.indexOf(" ") + 1; |
| 91 | 91 | ||
| 92 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
92 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
| 93 | 93 | ||
| 94 | var pos_split = full_name.indexOf("("); |
94 | var pos_split = full_name.indexOf("("); |
| 95 | 95 | ||
| 96 | var name = full_name.substr(0, pos_split); |
96 | var name = full_name.substr(0, pos_split); |
| 97 | 97 | ||
| 98 | var argument_string = full_name.substr(pos_split + 1); |
98 | var argument_string = full_name.substr(pos_split + 1); |
| 99 | 99 | ||
| 100 | if (argument_string.length > 0) |
100 | if (argument_string.length > 0) |
| 101 | { |
101 | { |
| 102 | argument_string = argument_string.replace(/,/g, ""); |
102 | argument_string = argument_string.replace(/,/g, ""); |
| 103 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
103 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
| 104 | } |
104 | } |
| 105 | 105 | ||
| 106 | Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string }; |
106 | Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string }; |
| 107 | return true; |
107 | return true; |
| 108 | } |
108 | } |
| 109 | 109 | ||
| 110 | function Console_StandardAutocomplete() |
110 | function Console_StandardAutocomplete() |
| 111 | { |
111 | { |
| 112 | var arg_index = arguments[0]; |
112 | var arg_index = arguments[0]; |
| 113 | var args = arguments[1]; |
113 | var args = arguments[1]; |
| 114 | 114 | ||
| 115 | if (0 <= arg_index && arg_index < arguments.length - 2) |
115 | if (0 <= arg_index && arg_index < arguments.length - 2) |
| 116 | { |
116 | { |
| 117 | return arguments[arg_index + 2]; |
117 | return arguments[arg_index + 2]; |
| 118 | } |
118 | } |
| 119 | 119 | ||
| 120 | return []; |
120 | return []; |
| 121 | } |
121 | } |
| 122 | 122 | ||
| 123 | // Default call function |
123 | // Default call function |
| 124 | 124 | ||
| 125 | function Console_Shell() |
125 | function Console_Shell() |
| 126 | { |
126 | { |
| 127 | var args = to_array(arguments); |
127 | var args = to_array(arguments); |
| 128 | var name = args.shift(); |
128 | var name = args.shift(); |
| 129 | 129 | ||
| 130 | if (Console_Functions[name] && Console_Functions[name]["command"]) |
130 | if (Console_Functions[name] && Console_Functions[name]["command"]) |
| 131 | { |
131 | { |
| 132 | return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0)); |
132 | return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0)); |
| 133 | } |
133 | } |
| 134 | 134 | ||
| 135 | return false; |
135 | return false; |
| 136 | } |
136 | } |
| 137 | Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
137 | Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
| 138 | 138 | ||
| 139 | // Help functions |
139 | // Help functions |
| 140 | 140 | ||
| 141 | function help_complete(arg_index, args) |
141 | function help_complete(arg_index, args) |
| 142 | { |
142 | { |
| 143 | var result = []; |
143 | var result = []; |
| 144 | 144 | ||
| 145 | if (arg_index == 1) // command name |
145 | if (arg_index == 1) // command name |
| 146 | { |
146 | { |
| 147 | for (var name in Console_Functions) |
147 | for (var name in Console_Functions) |
| 148 | { |
148 | { |
| 149 | result.push(name); |
149 | result.push(name); |
| 150 | } |
150 | } |
| 151 | } |
151 | } |
| 152 | 152 | ||
| 153 | return result; |
153 | return result; |
| 154 | } |
154 | } |
| 155 | 155 | ||
| 156 | function help(command_name) |
156 | function help(command_name) |
| 157 | { |
157 | { |
| 158 | if (!command_name) |
158 | if (!command_name) |
| 159 | { |
159 | { |
| 160 | command_name = "help"; |
160 | command_name = "help"; |
| 161 | } |
161 | } |
| 162 | 162 | ||
| 163 | if (Console_Functions[command_name] && Console_Functions[command_name]["help"]) |
163 | if (Console_Functions[command_name] && Console_Functions[command_name]["help"]) |
| 164 | { |
164 | { |
| 165 | var result = ""; |
165 | var result = ""; |
| 166 | 166 | ||
| 167 | if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"]) |
167 | if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"]) |
| 168 | { |
168 | { |
| 169 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
169 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
| 170 | } |
170 | } |
| 171 | 171 | ||
| 172 | result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n"; |
172 | result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n"; |
| 173 | 173 | ||
| 174 | Log(result); |
174 | Log(result); |
| 175 | return true; |
175 | return true; |
| 176 | } |
176 | } |
| 177 | 177 | ||
| 178 | Log(" |
178 | Log("\033[31mNo help for " + command_name + " found.\033[0m\n"); |
| 179 | return false; |
179 | return false; |
| 180 | } |
180 | } |
| 181 | Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
181 | Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); }); |
| 182 | 182 | ||