diff options
Diffstat (limited to 'OpenSim/Framework/Console/ConsoleBase.cs')
-rw-r--r-- | OpenSim/Framework/Console/ConsoleBase.cs | 661 |
1 files changed, 515 insertions, 146 deletions
diff --git a/OpenSim/Framework/Console/ConsoleBase.cs b/OpenSim/Framework/Console/ConsoleBase.cs index 30af23a..f990748 100644 --- a/OpenSim/Framework/Console/ConsoleBase.cs +++ b/OpenSim/Framework/Console/ConsoleBase.cs | |||
@@ -26,22 +26,250 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Text; |
30 | using System.Diagnostics; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | 30 | using System.Reflection; |
33 | using System.Text.RegularExpressions; | 31 | using System.Diagnostics; |
32 | using System.Collections.Generic; | ||
34 | using log4net; | 33 | using log4net; |
35 | 34 | ||
36 | namespace OpenSim.Framework.Console | 35 | namespace OpenSim.Framework.Console |
37 | { | 36 | { |
37 | public delegate void CommandDelegate(string module, string[] cmd); | ||
38 | |||
39 | public class Commands | ||
40 | { | ||
41 | private class CommandInfo | ||
42 | { | ||
43 | public string module; | ||
44 | public string help_text; | ||
45 | public string long_help; | ||
46 | public CommandDelegate fn; | ||
47 | } | ||
48 | |||
49 | private Dictionary<string, Object> tree = | ||
50 | new Dictionary<string, Object>(); | ||
51 | |||
52 | public List<string> GetHelp() | ||
53 | { | ||
54 | List<string> help = new List<string>(); | ||
55 | |||
56 | help.AddRange(CollectHelp(tree)); | ||
57 | |||
58 | help.Sort(); | ||
59 | |||
60 | return help; | ||
61 | } | ||
62 | |||
63 | private List<string> CollectHelp(Dictionary<string, Object> dict) | ||
64 | { | ||
65 | List<string> result = new List<string>(); | ||
66 | |||
67 | foreach (KeyValuePair<string, object> kvp in dict) | ||
68 | { | ||
69 | if (kvp.Value is Dictionary<string, Object>) | ||
70 | { | ||
71 | result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value)); | ||
72 | } | ||
73 | else | ||
74 | { | ||
75 | if (((CommandInfo)kvp.Value).long_help != String.Empty) | ||
76 | result.Add(((CommandInfo)kvp.Value).help_text+" - "+ | ||
77 | ((CommandInfo)kvp.Value).long_help); | ||
78 | } | ||
79 | } | ||
80 | return result; | ||
81 | } | ||
82 | |||
83 | public void AddCommand(string module, string command, string help, string longhelp, CommandDelegate fn) | ||
84 | { | ||
85 | string[] parts = Parser.Parse(command); | ||
86 | |||
87 | Dictionary<string, Object> current = tree; | ||
88 | foreach (string s in parts) | ||
89 | { | ||
90 | if (current.ContainsKey(s)) | ||
91 | { | ||
92 | if (current[s] is Dictionary<string, Object>) | ||
93 | { | ||
94 | current = (Dictionary<string, Object>)current[s]; | ||
95 | } | ||
96 | else | ||
97 | return; | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | current[s] = new Dictionary<string, Object>(); | ||
102 | current = (Dictionary<string, Object>)current[s]; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | if (current.ContainsKey(String.Empty)) | ||
107 | return; | ||
108 | CommandInfo info = new CommandInfo(); | ||
109 | info.module = module; | ||
110 | info.help_text = help; | ||
111 | info.long_help = longhelp; | ||
112 | info.fn = fn; | ||
113 | current[String.Empty] = info; | ||
114 | } | ||
115 | |||
116 | public string[] FindNextOption(string[] cmd, bool term) | ||
117 | { | ||
118 | Dictionary<string, object> current = tree; | ||
119 | |||
120 | int remaining = cmd.Length; | ||
121 | |||
122 | foreach (string s in cmd) | ||
123 | { | ||
124 | remaining--; | ||
125 | |||
126 | List<string> found = new List<string>(); | ||
127 | |||
128 | foreach (string opt in current.Keys) | ||
129 | { | ||
130 | if (opt.StartsWith(s)) | ||
131 | { | ||
132 | found.Add(opt); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | if (found.Count == 1 && (remaining != 0 || term)) | ||
137 | { | ||
138 | current = (Dictionary<string, object>)current[found[0]]; | ||
139 | } | ||
140 | else if (found.Count > 0) | ||
141 | { | ||
142 | return found.ToArray(); | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | break; | ||
147 | // return new string[] {"<cr>"}; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | if (current.Count > 1) | ||
152 | { | ||
153 | List<string> choices = new List<string>(); | ||
154 | |||
155 | bool addcr = false; | ||
156 | foreach (string s in current.Keys) | ||
157 | { | ||
158 | if (s == String.Empty) | ||
159 | { | ||
160 | CommandInfo ci = (CommandInfo)current[String.Empty]; | ||
161 | if (ci.fn != null) | ||
162 | addcr = true; | ||
163 | } | ||
164 | else | ||
165 | choices.Add(s); | ||
166 | } | ||
167 | if (addcr) | ||
168 | choices.Add("<cr>"); | ||
169 | return choices.ToArray(); | ||
170 | } | ||
171 | |||
172 | if (current.ContainsKey(String.Empty)) | ||
173 | return new string[] { "Command help: "+((CommandInfo)current[String.Empty]).help_text}; | ||
174 | |||
175 | return new string[] { new List<string>(current.Keys)[0] }; | ||
176 | } | ||
177 | |||
178 | public string[] Resolve(string[] cmd) | ||
179 | { | ||
180 | string[] result = cmd; | ||
181 | int index = -1; | ||
182 | |||
183 | Dictionary<string, object> current = tree; | ||
184 | |||
185 | foreach (string s in cmd) | ||
186 | { | ||
187 | index++; | ||
188 | |||
189 | List<string> found = new List<string>(); | ||
190 | |||
191 | foreach (string opt in current.Keys) | ||
192 | { | ||
193 | if (opt.StartsWith(s)) | ||
194 | { | ||
195 | found.Add(opt); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | if (found.Count == 1) | ||
200 | { | ||
201 | result[index] = found[0]; | ||
202 | current = (Dictionary<string, object>)current[found[0]]; | ||
203 | } | ||
204 | else if (found.Count > 0) | ||
205 | { | ||
206 | return new string[0]; | ||
207 | } | ||
208 | else | ||
209 | { | ||
210 | break; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | if (current.ContainsKey(String.Empty)) | ||
215 | { | ||
216 | CommandInfo ci = (CommandInfo)current[String.Empty]; | ||
217 | if (ci.fn == null) | ||
218 | return new string[0]; | ||
219 | ci.fn(ci.module, result); | ||
220 | return result; | ||
221 | } | ||
222 | return new string[0]; | ||
223 | } | ||
224 | } | ||
225 | |||
226 | public class Parser | ||
227 | { | ||
228 | public static string[] Parse(string text) | ||
229 | { | ||
230 | List<string> result = new List<string>(); | ||
231 | |||
232 | int index; | ||
233 | |||
234 | string[] unquoted = text.Split(new char[] {'"'}); | ||
235 | |||
236 | for (index = 0 ; index < unquoted.Length ; index++) | ||
237 | { | ||
238 | if (index % 2 == 0) | ||
239 | { | ||
240 | string[] words = unquoted[index].Split(new char[] {' '}); | ||
241 | foreach (string w in words) | ||
242 | { | ||
243 | if (w != String.Empty) | ||
244 | result.Add(w); | ||
245 | } | ||
246 | } | ||
247 | else | ||
248 | { | ||
249 | result.Add(unquoted[index]); | ||
250 | } | ||
251 | } | ||
252 | |||
253 | return result.ToArray(); | ||
254 | } | ||
255 | } | ||
256 | |||
38 | public class ConsoleBase | 257 | public class ConsoleBase |
39 | { | 258 | { |
40 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 259 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
41 | 260 | ||
42 | private readonly object m_syncRoot = new object(); | 261 | private readonly object m_syncRoot = new object(); |
43 | 262 | ||
44 | public conscmd_callback m_cmdParser; | 263 | private int y = -1; |
264 | private int cp = 0; | ||
265 | private int h = 1; | ||
266 | private string prompt = "# "; | ||
267 | private StringBuilder cmdline = new StringBuilder(); | ||
268 | public Commands Commands = new Commands(); | ||
269 | private bool echo = true; | ||
270 | private List<string> history = new List<string>(); | ||
271 | |||
272 | public object ConsoleScene = null; | ||
45 | 273 | ||
46 | /// <summary> | 274 | /// <summary> |
47 | /// The default prompt text. | 275 | /// The default prompt text. |
@@ -53,15 +281,19 @@ namespace OpenSim.Framework.Console | |||
53 | } | 281 | } |
54 | protected string m_defaultPrompt; | 282 | protected string m_defaultPrompt; |
55 | 283 | ||
56 | /// <summary> | 284 | public ConsoleBase(string defaultPrompt) |
57 | /// Constructor. | ||
58 | /// </summary> | ||
59 | /// <param name="defaultPrompt"></param> | ||
60 | /// <param name="cmdparser"></param> | ||
61 | public ConsoleBase(string defaultPrompt, conscmd_callback cmdparser) | ||
62 | { | 285 | { |
63 | DefaultPrompt = defaultPrompt; | 286 | DefaultPrompt = defaultPrompt; |
64 | m_cmdParser = cmdparser; | 287 | |
288 | Commands.AddCommand("console", "help", "help", "Get command list", Help); | ||
289 | } | ||
290 | |||
291 | private void AddToHistory(string text) | ||
292 | { | ||
293 | while (history.Count >= 100) | ||
294 | history.RemoveAt(0); | ||
295 | |||
296 | history.Add(text); | ||
65 | } | 297 | } |
66 | 298 | ||
67 | /// <summary> | 299 | /// <summary> |
@@ -95,8 +327,7 @@ namespace OpenSim.Framework.Console | |||
95 | /// <param name="args">WriteLine-style message arguments</param> | 327 | /// <param name="args">WriteLine-style message arguments</param> |
96 | public void Warn(string sender, string format, params object[] args) | 328 | public void Warn(string sender, string format, params object[] args) |
97 | { | 329 | { |
98 | WritePrefixLine(DeriveColor(sender), sender); | 330 | WriteNewLine(DeriveColor(sender), sender, ConsoleColor.Yellow, format, args); |
99 | WriteNewLine(ConsoleColor.Yellow, format, args); | ||
100 | } | 331 | } |
101 | 332 | ||
102 | /// <summary> | 333 | /// <summary> |
@@ -117,10 +348,8 @@ namespace OpenSim.Framework.Console | |||
117 | /// <param name="args">WriteLine-style message arguments</param> | 348 | /// <param name="args">WriteLine-style message arguments</param> |
118 | public void Notice(string sender, string format, params object[] args) | 349 | public void Notice(string sender, string format, params object[] args) |
119 | { | 350 | { |
120 | WritePrefixLine(DeriveColor(sender), sender); | 351 | WriteNewLine(DeriveColor(sender), sender, ConsoleColor.White, format, args); |
121 | WriteNewLine(ConsoleColor.White, format, args); | ||
122 | } | 352 | } |
123 | |||
124 | /// <summary> | 353 | /// <summary> |
125 | /// Sends an error to the current console output | 354 | /// Sends an error to the current console output |
126 | /// </summary> | 355 | /// </summary> |
@@ -139,8 +368,7 @@ namespace OpenSim.Framework.Console | |||
139 | /// <param name="args">WriteLine-style message arguments</param> | 368 | /// <param name="args">WriteLine-style message arguments</param> |
140 | public void Error(string sender, string format, params object[] args) | 369 | public void Error(string sender, string format, params object[] args) |
141 | { | 370 | { |
142 | WritePrefixLine(DeriveColor(sender), sender); | 371 | WriteNewLine(DeriveColor(sender), sender, ConsoleColor.Red, format, args); |
143 | Error(format, args); | ||
144 | } | 372 | } |
145 | 373 | ||
146 | /// <summary> | 374 | /// <summary> |
@@ -161,8 +389,7 @@ namespace OpenSim.Framework.Console | |||
161 | /// <param name="args">WriteLine-style message arguments</param> | 389 | /// <param name="args">WriteLine-style message arguments</param> |
162 | public void Status(string sender, string format, params object[] args) | 390 | public void Status(string sender, string format, params object[] args) |
163 | { | 391 | { |
164 | WritePrefixLine(DeriveColor(sender), sender); | 392 | WriteNewLine(DeriveColor(sender), sender, ConsoleColor.Blue, format, args); |
165 | WriteNewLine(ConsoleColor.Blue, format, args); | ||
166 | } | 393 | } |
167 | 394 | ||
168 | [Conditional("DEBUG")] | 395 | [Conditional("DEBUG")] |
@@ -174,12 +401,60 @@ namespace OpenSim.Framework.Console | |||
174 | [Conditional("DEBUG")] | 401 | [Conditional("DEBUG")] |
175 | public void Debug(string sender, string format, params object[] args) | 402 | public void Debug(string sender, string format, params object[] args) |
176 | { | 403 | { |
177 | WritePrefixLine(DeriveColor(sender), sender); | 404 | WriteNewLine(DeriveColor(sender), sender, ConsoleColor.Gray, format, args); |
178 | WriteNewLine(ConsoleColor.Gray, format, args); | 405 | } |
406 | |||
407 | private void WriteNewLine(ConsoleColor senderColor, string sender, ConsoleColor color, string format, params object[] args) | ||
408 | { | ||
409 | lock (cmdline) | ||
410 | { | ||
411 | if (y != -1) | ||
412 | { | ||
413 | System.Console.CursorTop = y; | ||
414 | System.Console.CursorLeft = 0; | ||
415 | |||
416 | int count = cmdline.Length; | ||
417 | |||
418 | System.Console.Write(" "); | ||
419 | while (count-- > 0) | ||
420 | System.Console.Write(" "); | ||
421 | |||
422 | System.Console.CursorTop = y; | ||
423 | System.Console.CursorLeft = 0; | ||
424 | } | ||
425 | WritePrefixLine(senderColor, sender); | ||
426 | WriteConsoleLine(color, format, args); | ||
427 | if (y != -1) | ||
428 | y = System.Console.CursorTop; | ||
429 | } | ||
179 | } | 430 | } |
180 | 431 | ||
181 | private void WriteNewLine(ConsoleColor color, string format, params object[] args) | 432 | private void WriteNewLine(ConsoleColor color, string format, params object[] args) |
182 | { | 433 | { |
434 | lock (cmdline) | ||
435 | { | ||
436 | if (y != -1) | ||
437 | { | ||
438 | System.Console.CursorTop = y; | ||
439 | System.Console.CursorLeft = 0; | ||
440 | |||
441 | int count = cmdline.Length; | ||
442 | |||
443 | System.Console.Write(" "); | ||
444 | while (count-- > 0) | ||
445 | System.Console.Write(" "); | ||
446 | |||
447 | System.Console.CursorTop = y; | ||
448 | System.Console.CursorLeft = 0; | ||
449 | } | ||
450 | WriteConsoleLine(color, format, args); | ||
451 | if (y != -1) | ||
452 | y = System.Console.CursorTop; | ||
453 | } | ||
454 | } | ||
455 | |||
456 | private void WriteConsoleLine(ConsoleColor color, string format, params object[] args) | ||
457 | { | ||
183 | try | 458 | try |
184 | { | 459 | { |
185 | lock (m_syncRoot) | 460 | lock (m_syncRoot) |
@@ -240,108 +515,150 @@ namespace OpenSim.Framework.Console | |||
240 | } | 515 | } |
241 | } | 516 | } |
242 | 517 | ||
243 | public string ReadLine() | 518 | private void Help(string module, string[] cmd) |
244 | { | 519 | { |
245 | try | 520 | List<string> help = Commands.GetHelp(); |
246 | { | 521 | |
247 | string line = System.Console.ReadLine(); | 522 | foreach (string s in help) |
523 | Output(s); | ||
524 | } | ||
248 | 525 | ||
249 | while (line == null) | 526 | private void Show() |
527 | { | ||
528 | lock (cmdline) | ||
529 | { | ||
530 | if (y == -1 || System.Console.BufferWidth == 0) | ||
531 | return; | ||
532 | |||
533 | int xc = prompt.Length + cp; | ||
534 | int new_x = xc % System.Console.BufferWidth; | ||
535 | int new_y = y + xc / System.Console.BufferWidth; | ||
536 | int end_y = y + (cmdline.Length + prompt.Length) / System.Console.BufferWidth; | ||
537 | if (end_y / System.Console.BufferWidth >= h) | ||
538 | h++; | ||
539 | if (end_y >= System.Console.BufferHeight) // wrap | ||
250 | { | 540 | { |
251 | line = System.Console.ReadLine(); | 541 | y--; |
542 | new_y--; | ||
543 | System.Console.CursorLeft = 0; | ||
544 | System.Console.CursorTop = System.Console.BufferHeight-1; | ||
545 | System.Console.WriteLine(" "); | ||
252 | } | 546 | } |
253 | 547 | ||
254 | return line; | 548 | System.Console.CursorTop = y; |
255 | } | 549 | System.Console.CursorLeft = 0; |
256 | catch (Exception e) | ||
257 | { | ||
258 | m_log.Error("[Console]: System.Console.ReadLine exception " + e.ToString()); | ||
259 | return String.Empty; | ||
260 | } | ||
261 | } | ||
262 | 550 | ||
263 | public int Read() | 551 | if (echo) |
264 | { | 552 | System.Console.Write("{0}{1}", prompt, cmdline); |
265 | return System.Console.Read(); | 553 | else |
554 | System.Console.Write("{0}", prompt); | ||
555 | |||
556 | System.Console.CursorLeft = new_x; | ||
557 | System.Console.CursorTop = new_y; | ||
558 | } | ||
266 | } | 559 | } |
267 | 560 | ||
268 | public IPAddress CmdPromptIPAddress(string prompt, string defaultvalue) | 561 | public void LockOutput() |
269 | { | 562 | { |
270 | IPAddress address; | 563 | System.Threading.Monitor.Enter(cmdline); |
271 | string addressStr; | 564 | try |
272 | |||
273 | while (true) | ||
274 | { | 565 | { |
275 | addressStr = CmdPrompt(prompt, defaultvalue); | 566 | if (y != -1) |
276 | if (IPAddress.TryParse(addressStr, out address)) | ||
277 | { | 567 | { |
278 | break; | 568 | System.Console.CursorTop = y; |
279 | } | 569 | System.Console.CursorLeft = 0; |
280 | else | 570 | |
281 | { | 571 | int count = cmdline.Length + prompt.Length; |
282 | m_log.Error("Illegal address. Please re-enter."); | 572 | |
573 | while (count-- > 0) | ||
574 | System.Console.Write(" "); | ||
575 | |||
576 | System.Console.CursorTop = y; | ||
577 | System.Console.CursorLeft = 0; | ||
578 | |||
283 | } | 579 | } |
284 | } | 580 | } |
285 | 581 | catch (Exception) | |
286 | return address; | 582 | { |
583 | } | ||
287 | } | 584 | } |
288 | 585 | ||
289 | public uint CmdPromptIPPort(string prompt, string defaultvalue) | 586 | public void UnlockOutput() |
290 | { | 587 | { |
291 | uint port; | 588 | if (y != -1) |
292 | string portStr; | 589 | { |
590 | y = System.Console.CursorTop; | ||
591 | Show(); | ||
592 | } | ||
593 | System.Threading.Monitor.Exit(cmdline); | ||
594 | } | ||
293 | 595 | ||
294 | while (true) | 596 | public void Output(string text) |
597 | { | ||
598 | lock (cmdline) | ||
295 | { | 599 | { |
296 | portStr = CmdPrompt(prompt, defaultvalue); | 600 | if (y == -1) |
297 | if (uint.TryParse(portStr, out port)) | ||
298 | { | 601 | { |
299 | if (port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort) | 602 | System.Console.WriteLine(text); |
300 | { | 603 | |
301 | break; | 604 | return; |
302 | } | ||
303 | } | 605 | } |
304 | 606 | ||
305 | m_log.Error("Illegal address. Please re-enter."); | 607 | System.Console.CursorTop = y; |
306 | } | 608 | System.Console.CursorLeft = 0; |
307 | 609 | ||
308 | return port; | 610 | int count = cmdline.Length + prompt.Length; |
309 | } | ||
310 | 611 | ||
311 | // Displays a prompt and waits for the user to enter a string, then returns that string | 612 | while (count-- > 0) |
312 | // (Done with no echo and suitable for passwords - currently disabled) | 613 | System.Console.Write(" "); |
313 | public string PasswdPrompt(string prompt) | 614 | |
314 | { | 615 | System.Console.CursorTop = y; |
315 | // FIXME: Needs to be better abstracted | 616 | System.Console.CursorLeft = 0; |
316 | System.Console.WriteLine(String.Format("{0}: ", prompt)); | 617 | |
317 | //ConsoleColor oldfg = System.Console.ForegroundColor; | 618 | System.Console.WriteLine(text); |
318 | //System.Console.ForegroundColor = System.Console.BackgroundColor; | 619 | |
319 | string temp = System.Console.ReadLine(); | 620 | y = System.Console.CursorTop; |
320 | //System.Console.ForegroundColor = oldfg; | 621 | |
321 | return temp; | 622 | Show(); |
623 | } | ||
322 | } | 624 | } |
323 | 625 | ||
324 | // Displays a command prompt and waits for the user to enter a string, then returns that string | 626 | private void ContextHelp() |
325 | public string CmdPrompt(string prompt) | ||
326 | { | 627 | { |
327 | System.Console.WriteLine(String.Format("{0}: ", prompt)); | 628 | string[] words = Parser.Parse(cmdline.ToString()); |
328 | return ReadLine(); | 629 | |
630 | string[] opts = Commands.FindNextOption(words, cmdline.ToString().EndsWith(" ")); | ||
631 | |||
632 | if (opts[0].StartsWith("Command help:")) | ||
633 | Output(opts[0]); | ||
634 | else | ||
635 | Output(String.Format("Options: {0}", String.Join(" ", opts))); | ||
329 | } | 636 | } |
330 | 637 | ||
331 | // Displays a command prompt and returns a default value if the user simply presses enter | 638 | public void Prompt() |
332 | public string CmdPrompt(string prompt, string defaultresponse) | ||
333 | { | 639 | { |
334 | string temp = CmdPrompt(String.Format("{0} [{1}]", prompt, defaultresponse)); | 640 | string line = ReadLine(m_defaultPrompt, true, true); |
335 | if (temp == String.Empty) | 641 | |
336 | { | 642 | if (line != String.Empty) |
337 | return defaultresponse; | ||
338 | } | ||
339 | else | ||
340 | { | 643 | { |
341 | return temp; | 644 | m_log.Info("Invalid command"); |
342 | } | 645 | } |
343 | } | 646 | } |
344 | 647 | ||
648 | public string CmdPrompt(string p) | ||
649 | { | ||
650 | return ReadLine(String.Format("{0}: ", p), false, true); | ||
651 | } | ||
652 | |||
653 | public string CmdPrompt(string p, string def) | ||
654 | { | ||
655 | string ret = ReadLine(String.Format("{0} [{1}]: ", p, def), false, true); | ||
656 | if (ret == String.Empty) | ||
657 | ret = def; | ||
658 | |||
659 | return ret; | ||
660 | } | ||
661 | |||
345 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options | 662 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options |
346 | public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB) | 663 | public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB) |
347 | { | 664 | { |
@@ -362,85 +679,137 @@ namespace OpenSim.Framework.Console | |||
362 | return temp; | 679 | return temp; |
363 | } | 680 | } |
364 | 681 | ||
365 | // Runs a command with a number of parameters | 682 | // Displays a prompt and waits for the user to enter a string, then returns that string |
366 | public Object RunCmd(string Cmd, string[] cmdparams) | 683 | // (Done with no echo and suitable for passwords) |
367 | { | 684 | public string PasswdPrompt(string p) |
368 | m_cmdParser.RunCmd(Cmd, cmdparams); | ||
369 | return null; | ||
370 | } | ||
371 | |||
372 | // Shows data about something | ||
373 | public void ShowCommands(string ShowWhat) | ||
374 | { | 685 | { |
375 | m_cmdParser.Show(new string[] { ShowWhat }); | 686 | return ReadLine(p, false, false); |
376 | } | 687 | } |
377 | 688 | ||
378 | public void Prompt() | 689 | public void RunCommand(string cmd) |
379 | { | 690 | { |
380 | string tempstr = CmdPrompt(m_defaultPrompt); | 691 | string[] parts = Parser.Parse(cmd); |
381 | RunCommand(tempstr); | 692 | Commands.Resolve(parts); |
382 | } | 693 | } |
383 | 694 | ||
384 | public void RunCommand(string cmdline) | 695 | public string ReadLine(string p, bool isCommand, bool e) |
385 | { | 696 | { |
386 | Regex Extractor = new Regex(@"(['""][^""]+['""])\s*|([^\s]+)\s*", RegexOptions.Compiled); | 697 | h = 1; |
387 | char[] delims = {' ', '"'}; | 698 | cp = 0; |
388 | MatchCollection matches = Extractor.Matches(cmdline); | 699 | prompt = p; |
389 | // Get matches | 700 | echo = e; |
701 | int historyLine = history.Count; | ||
390 | 702 | ||
391 | if (matches.Count == 0) | 703 | System.Console.CursorLeft = 0; // Needed for mono |
392 | return; | 704 | System.Console.Write(" "); // Needed for mono |
393 | 705 | ||
394 | string cmd = matches[0].Value.Trim(delims); | 706 | y = System.Console.CursorTop; |
395 | string[] cmdparams = new string[matches.Count - 1]; | 707 | cmdline = new StringBuilder(); |
396 | 708 | ||
397 | for (int i = 1; i < matches.Count; i++) | 709 | while(true) |
398 | { | 710 | { |
399 | cmdparams[i-1] = matches[i].Value.Trim(delims); | 711 | Show(); |
400 | } | ||
401 | 712 | ||
402 | try | 713 | ConsoleKeyInfo key = System.Console.ReadKey(true); |
403 | { | 714 | char c = key.KeyChar; |
404 | RunCmd(cmd, cmdparams); | ||
405 | } | ||
406 | catch (Exception e) | ||
407 | { | ||
408 | m_log.ErrorFormat("[Console]: Command [{0}] failed with exception {1}", cmdline, e.ToString()); | ||
409 | m_log.Error(e.StackTrace); | ||
410 | } | ||
411 | } | ||
412 | 715 | ||
413 | public string LineInfo | 716 | if (!Char.IsControl(c)) |
414 | { | 717 | { |
415 | get | 718 | if (cp >= 318) |
416 | { | 719 | continue; |
417 | string result = String.Empty; | ||
418 | 720 | ||
419 | string stacktrace = Environment.StackTrace; | 721 | if (c == '?' && isCommand) |
420 | List<string> lines = new List<string>(stacktrace.Split(new string[] {"at "}, StringSplitOptions.None)); | 722 | { |
723 | ContextHelp(); | ||
724 | continue; | ||
725 | } | ||
421 | 726 | ||
422 | if (lines.Count > 4) | 727 | cmdline.Insert(cp, c); |
728 | cp++; | ||
729 | } | ||
730 | else | ||
423 | { | 731 | { |
424 | lines.RemoveRange(0, 4); | 732 | switch (key.Key) |
733 | { | ||
734 | case ConsoleKey.Backspace: | ||
735 | if (cp == 0) | ||
736 | break; | ||
737 | cmdline.Remove(cp-1, 1); | ||
738 | cp--; | ||
425 | 739 | ||
426 | string tmpLine = lines[0]; | 740 | System.Console.CursorLeft = 0; |
741 | System.Console.CursorTop = y; | ||
427 | 742 | ||
428 | int inIndex = tmpLine.IndexOf(" in "); | 743 | System.Console.Write("{0}{1} ", prompt, cmdline); |
429 | 744 | ||
430 | if (inIndex > -1) | 745 | break; |
431 | { | 746 | case ConsoleKey.End: |
432 | result = tmpLine.Substring(0, inIndex); | 747 | cp = cmdline.Length; |
748 | break; | ||
749 | case ConsoleKey.Home: | ||
750 | cp = 0; | ||
751 | break; | ||
752 | case ConsoleKey.UpArrow: | ||
753 | if (historyLine < 1) | ||
754 | break; | ||
755 | historyLine--; | ||
756 | LockOutput(); | ||
757 | cmdline = new StringBuilder(history[historyLine]); | ||
758 | cp = cmdline.Length; | ||
759 | UnlockOutput(); | ||
760 | break; | ||
761 | case ConsoleKey.DownArrow: | ||
762 | if (historyLine >= history.Count) | ||
763 | break; | ||
764 | historyLine++; | ||
765 | LockOutput(); | ||
766 | if (historyLine == history.Count) | ||
767 | cmdline = new StringBuilder(); | ||
768 | else | ||
769 | cmdline = new StringBuilder(history[historyLine]); | ||
770 | cp = cmdline.Length; | ||
771 | UnlockOutput(); | ||
772 | break; | ||
773 | case ConsoleKey.LeftArrow: | ||
774 | if (cp > 0) | ||
775 | cp--; | ||
776 | break; | ||
777 | case ConsoleKey.RightArrow: | ||
778 | if (cp < cmdline.Length) | ||
779 | cp++; | ||
780 | break; | ||
781 | case ConsoleKey.Enter: | ||
782 | System.Console.CursorLeft = 0; | ||
783 | System.Console.CursorTop = y; | ||
784 | |||
785 | System.Console.WriteLine("{0}{1}", prompt, cmdline); | ||
433 | 786 | ||
434 | int lineIndex = tmpLine.IndexOf(":line "); | 787 | y = -1; |
435 | 788 | ||
436 | if (lineIndex > -1) | 789 | if (isCommand) |
437 | { | 790 | { |
438 | lineIndex += 6; | 791 | string[] cmd = Commands.Resolve(Parser.Parse(cmdline.ToString())); |
439 | result += ", line " + tmpLine.Substring(lineIndex, (tmpLine.Length - lineIndex) - 5); | 792 | |
793 | if (cmd.Length != 0) | ||
794 | { | ||
795 | int i; | ||
796 | |||
797 | for (i=0 ; i < cmd.Length ; i++) | ||
798 | { | ||
799 | if (cmd[i].Contains(" ")) | ||
800 | cmd[i] = "\"" + cmd[i] + "\""; | ||
801 | } | ||
802 | AddToHistory(String.Join(" ", cmd)); | ||
803 | return String.Empty; | ||
804 | } | ||
440 | } | 805 | } |
806 | |||
807 | AddToHistory(cmdline.ToString()); | ||
808 | return cmdline.ToString(); | ||
809 | default: | ||
810 | break; | ||
441 | } | 811 | } |
442 | } | 812 | } |
443 | return result; | ||
444 | } | 813 | } |
445 | } | 814 | } |
446 | } | 815 | } |