CommandConsole.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Xml;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.Linq;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Threading;
  36. using log4net;
  37. using OpenSim.Framework;
  38. namespace OpenSim.Framework.Console
  39. {
  40. public class Commands : ICommands
  41. {
  42. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// Encapsulates a command that can be invoked from the console
  45. /// </summary>
  46. private class CommandInfo
  47. {
  48. /// <value>
  49. /// The module from which this command comes
  50. /// </value>
  51. public string module;
  52. /// <value>
  53. /// Whether the module is shared
  54. /// </value>
  55. public bool shared;
  56. /// <value>
  57. /// Very short BNF description
  58. /// </value>
  59. public string help_text;
  60. /// <value>
  61. /// Longer one line help text
  62. /// </value>
  63. public string long_help;
  64. /// <value>
  65. /// Full descriptive help for this command
  66. /// </value>
  67. public string descriptive_help;
  68. /// <value>
  69. /// The method to invoke for this command
  70. /// </value>
  71. public List<CommandDelegate> fn;
  72. }
  73. public const string GeneralHelpText
  74. = "To enter an argument that contains spaces, surround the argument with double quotes.\nFor example, show object name \"My long object name\"\n";
  75. public const string ItemHelpText
  76. = @"For more information, type 'help all' to get a list of all commands,
  77. or type help <item>' where <item> is one of the following:";
  78. /// <value>
  79. /// Commands organized by keyword in a tree
  80. /// </value>
  81. private Dictionary<string, object> tree =
  82. new Dictionary<string, object>();
  83. /// <summary>
  84. /// Commands organized by module
  85. /// </summary>
  86. private Dictionary<string, List<CommandInfo>> m_modulesCommands = new Dictionary<string, List<CommandInfo>>();
  87. /// <summary>
  88. /// Get help for the given help string
  89. /// </summary>
  90. /// <param name="helpParts">Parsed parts of the help string. If empty then general help is returned.</param>
  91. /// <returns></returns>
  92. public List<string> GetHelp(string[] cmd)
  93. {
  94. List<string> help = new List<string>();
  95. List<string> helpParts = new List<string>(cmd);
  96. // Remove initial help keyword
  97. helpParts.RemoveAt(0);
  98. help.Add(""); // Will become a newline.
  99. // General help
  100. if (helpParts.Count == 0)
  101. {
  102. help.Add(GeneralHelpText);
  103. help.Add(ItemHelpText);
  104. help.AddRange(CollectModulesHelp(tree));
  105. }
  106. else if (helpParts.Count == 1 && helpParts[0] == "all")
  107. {
  108. help.AddRange(CollectAllCommandsHelp());
  109. }
  110. else
  111. {
  112. help.AddRange(CollectHelp(helpParts));
  113. }
  114. help.Add(""); // Will become a newline.
  115. return help;
  116. }
  117. /// <summary>
  118. /// Collects the help from all commands and return in alphabetical order.
  119. /// </summary>
  120. /// <returns></returns>
  121. private List<string> CollectAllCommandsHelp()
  122. {
  123. List<string> help = new List<string>();
  124. lock (m_modulesCommands)
  125. {
  126. foreach (List<CommandInfo> commands in m_modulesCommands.Values)
  127. {
  128. var ourHelpText = commands.ConvertAll(c => string.Format("{0} - {1}", c.help_text, c.long_help));
  129. help.AddRange(ourHelpText);
  130. }
  131. }
  132. help.Sort();
  133. return help;
  134. }
  135. /// <summary>
  136. /// See if we can find the requested command in order to display longer help
  137. /// </summary>
  138. /// <param name="helpParts"></param>
  139. /// <returns></returns>
  140. private List<string> CollectHelp(List<string> helpParts)
  141. {
  142. string originalHelpRequest = string.Join(" ", helpParts.ToArray());
  143. List<string> help = new List<string>();
  144. // Check modules first to see if we just need to display a list of those commands
  145. if (TryCollectModuleHelp(originalHelpRequest, help))
  146. {
  147. help.Insert(0, ItemHelpText);
  148. return help;
  149. }
  150. Dictionary<string, object> dict = tree;
  151. while (helpParts.Count > 0)
  152. {
  153. string helpPart = helpParts[0];
  154. if (!dict.ContainsKey(helpPart))
  155. break;
  156. //m_log.Debug("Found {0}", helpParts[0]);
  157. if (dict[helpPart] is Dictionary<string, Object>)
  158. dict = (Dictionary<string, object>)dict[helpPart];
  159. helpParts.RemoveAt(0);
  160. }
  161. // There was a command for the given help string
  162. if (dict.ContainsKey(String.Empty))
  163. {
  164. CommandInfo commandInfo = (CommandInfo)dict[String.Empty];
  165. help.Add(commandInfo.help_text);
  166. help.Add(commandInfo.long_help);
  167. string descriptiveHelp = commandInfo.descriptive_help;
  168. // If we do have some descriptive help then insert a spacing line before for readability.
  169. if (descriptiveHelp != string.Empty)
  170. help.Add(string.Empty);
  171. help.Add(commandInfo.descriptive_help);
  172. }
  173. else
  174. {
  175. help.Add(string.Format("No help is available for {0}", originalHelpRequest));
  176. }
  177. return help;
  178. }
  179. /// <summary>
  180. /// Try to collect help for the given module if that module exists.
  181. /// </summary>
  182. /// <param name="moduleName"></param>
  183. /// <param name="helpText">/param>
  184. /// <returns>true if there was the module existed, false otherwise.</returns>
  185. private bool TryCollectModuleHelp(string moduleName, List<string> helpText)
  186. {
  187. lock (m_modulesCommands)
  188. {
  189. foreach (string key in m_modulesCommands.Keys)
  190. {
  191. // Allow topic help requests to succeed whether they are upper or lowercase.
  192. if (moduleName.ToLower() == key.ToLower())
  193. {
  194. List<CommandInfo> commands = m_modulesCommands[key];
  195. var ourHelpText = commands.ConvertAll(c => string.Format("{0} - {1}", c.help_text, c.long_help));
  196. ourHelpText.Sort();
  197. helpText.AddRange(ourHelpText);
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. }
  204. private List<string> CollectModulesHelp(Dictionary<string, object> dict)
  205. {
  206. lock (m_modulesCommands)
  207. {
  208. List<string> helpText = new List<string>(m_modulesCommands.Keys);
  209. helpText.Sort();
  210. return helpText;
  211. }
  212. }
  213. // private List<string> CollectHelp(Dictionary<string, object> dict)
  214. // {
  215. // List<string> result = new List<string>();
  216. //
  217. // foreach (KeyValuePair<string, object> kvp in dict)
  218. // {
  219. // if (kvp.Value is Dictionary<string, Object>)
  220. // {
  221. // result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value));
  222. // }
  223. // else
  224. // {
  225. // if (((CommandInfo)kvp.Value).long_help != String.Empty)
  226. // result.Add(((CommandInfo)kvp.Value).help_text+" - "+
  227. // ((CommandInfo)kvp.Value).long_help);
  228. // }
  229. // }
  230. // return result;
  231. // }
  232. /// <summary>
  233. /// Add a command to those which can be invoked from the console.
  234. /// </summary>
  235. /// <param name="module"></param>
  236. /// <param name="command"></param>
  237. /// <param name="help"></param>
  238. /// <param name="longhelp"></param>
  239. /// <param name="fn"></param>
  240. public void AddCommand(string module, bool shared, string command,
  241. string help, string longhelp, CommandDelegate fn)
  242. {
  243. AddCommand(module, shared, command, help, longhelp, String.Empty, fn);
  244. }
  245. /// <summary>
  246. /// Add a command to those which can be invoked from the console.
  247. /// </summary>
  248. /// <param name="module"></param>
  249. /// <param name="command"></param>
  250. /// <param name="help"></param>
  251. /// <param name="longhelp"></param>
  252. /// <param name="descriptivehelp"></param>
  253. /// <param name="fn"></param>
  254. public void AddCommand(string module, bool shared, string command,
  255. string help, string longhelp, string descriptivehelp,
  256. CommandDelegate fn)
  257. {
  258. string[] parts = Parser.Parse(command);
  259. Dictionary<string, Object> current = tree;
  260. foreach (string part in parts)
  261. {
  262. if (current.ContainsKey(part))
  263. {
  264. if (current[part] is Dictionary<string, Object>)
  265. current = (Dictionary<string, Object>)current[part];
  266. else
  267. return;
  268. }
  269. else
  270. {
  271. current[part] = new Dictionary<string, Object>();
  272. current = (Dictionary<string, Object>)current[part];
  273. }
  274. }
  275. CommandInfo info;
  276. if (current.ContainsKey(String.Empty))
  277. {
  278. info = (CommandInfo)current[String.Empty];
  279. if (!info.shared && !info.fn.Contains(fn))
  280. info.fn.Add(fn);
  281. return;
  282. }
  283. info = new CommandInfo();
  284. info.module = module;
  285. info.shared = shared;
  286. info.help_text = help;
  287. info.long_help = longhelp;
  288. info.descriptive_help = descriptivehelp;
  289. info.fn = new List<CommandDelegate>();
  290. info.fn.Add(fn);
  291. current[String.Empty] = info;
  292. // Now add command to modules dictionary
  293. lock (m_modulesCommands)
  294. {
  295. List<CommandInfo> commands;
  296. if (m_modulesCommands.ContainsKey(module))
  297. {
  298. commands = m_modulesCommands[module];
  299. }
  300. else
  301. {
  302. commands = new List<CommandInfo>();
  303. m_modulesCommands[module] = commands;
  304. }
  305. // m_log.DebugFormat("[COMMAND CONSOLE]: Adding to category {0} command {1}", module, command);
  306. commands.Add(info);
  307. }
  308. }
  309. public string[] FindNextOption(string[] cmd, bool term)
  310. {
  311. Dictionary<string, object> current = tree;
  312. int remaining = cmd.Length;
  313. foreach (string s in cmd)
  314. {
  315. remaining--;
  316. List<string> found = new List<string>();
  317. foreach (string opt in current.Keys)
  318. {
  319. if (remaining > 0 && opt == s)
  320. {
  321. found.Clear();
  322. found.Add(opt);
  323. break;
  324. }
  325. if (opt.StartsWith(s))
  326. {
  327. found.Add(opt);
  328. }
  329. }
  330. if (found.Count == 1 && (remaining != 0 || term))
  331. {
  332. current = (Dictionary<string, object>)current[found[0]];
  333. }
  334. else if (found.Count > 0)
  335. {
  336. return found.ToArray();
  337. }
  338. else
  339. {
  340. break;
  341. // return new string[] {"<cr>"};
  342. }
  343. }
  344. if (current.Count > 1)
  345. {
  346. List<string> choices = new List<string>();
  347. bool addcr = false;
  348. foreach (string s in current.Keys)
  349. {
  350. if (s == String.Empty)
  351. {
  352. CommandInfo ci = (CommandInfo)current[String.Empty];
  353. if (ci.fn.Count != 0)
  354. addcr = true;
  355. }
  356. else
  357. choices.Add(s);
  358. }
  359. if (addcr)
  360. choices.Add("<cr>");
  361. return choices.ToArray();
  362. }
  363. if (current.ContainsKey(String.Empty))
  364. return new string[] { "Command help: "+((CommandInfo)current[String.Empty]).help_text};
  365. return new string[] { new List<string>(current.Keys)[0] };
  366. }
  367. private CommandInfo ResolveCommand(string[] cmd, out string[] result)
  368. {
  369. result = cmd;
  370. int index = -1;
  371. Dictionary<string, object> current = tree;
  372. foreach (string s in cmd)
  373. {
  374. index++;
  375. List<string> found = new List<string>();
  376. foreach (string opt in current.Keys)
  377. {
  378. if (opt == s)
  379. {
  380. found.Clear();
  381. found.Add(opt);
  382. break;
  383. }
  384. if (opt.StartsWith(s))
  385. {
  386. found.Add(opt);
  387. }
  388. }
  389. if (found.Count == 1)
  390. {
  391. result[index] = found[0];
  392. current = (Dictionary<string, object>)current[found[0]];
  393. }
  394. else if (found.Count > 0)
  395. {
  396. return null;
  397. }
  398. else
  399. {
  400. break;
  401. }
  402. }
  403. if (current.ContainsKey(String.Empty))
  404. return (CommandInfo)current[String.Empty];
  405. return null;
  406. }
  407. public bool HasCommand(string command)
  408. {
  409. string[] result;
  410. return ResolveCommand(Parser.Parse(command), out result) != null;
  411. }
  412. public string[] Resolve(string[] cmd)
  413. {
  414. string[] result;
  415. CommandInfo ci = ResolveCommand(cmd, out result);
  416. if (ci == null)
  417. return new string[0];
  418. if (ci.fn.Count == 0)
  419. return new string[0];
  420. foreach (CommandDelegate fn in ci.fn)
  421. {
  422. if (fn != null)
  423. fn(ci.module, result);
  424. else
  425. return new string[0];
  426. }
  427. return result;
  428. }
  429. public XmlElement GetXml(XmlDocument doc)
  430. {
  431. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  432. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  433. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  434. tree.Remove("help");
  435. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  436. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  437. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  438. tree.Remove("quit");
  439. XmlElement root = doc.CreateElement("", "HelpTree", "");
  440. ProcessTreeLevel(tree, root, doc);
  441. if (!tree.ContainsKey("help"))
  442. tree["help"] = (object) new Dictionary<string, object>();
  443. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  444. if (!tree.ContainsKey("quit"))
  445. tree["quit"] = (object) new Dictionary<string, object>();
  446. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  447. return root;
  448. }
  449. private void ProcessTreeLevel(Dictionary<string, object> level, XmlElement xml, XmlDocument doc)
  450. {
  451. foreach (KeyValuePair<string, object> kvp in level)
  452. {
  453. if (kvp.Value is Dictionary<string, Object>)
  454. {
  455. XmlElement next = doc.CreateElement("", "Level", "");
  456. next.SetAttribute("Name", kvp.Key);
  457. xml.AppendChild(next);
  458. ProcessTreeLevel((Dictionary<string, object>)kvp.Value, next, doc);
  459. }
  460. else
  461. {
  462. CommandInfo c = (CommandInfo)kvp.Value;
  463. XmlElement cmd = doc.CreateElement("", "Command", "");
  464. XmlElement e;
  465. e = doc.CreateElement("", "Module", "");
  466. cmd.AppendChild(e);
  467. e.AppendChild(doc.CreateTextNode(c.module));
  468. e = doc.CreateElement("", "Shared", "");
  469. cmd.AppendChild(e);
  470. e.AppendChild(doc.CreateTextNode(c.shared.ToString()));
  471. e = doc.CreateElement("", "HelpText", "");
  472. cmd.AppendChild(e);
  473. e.AppendChild(doc.CreateTextNode(c.help_text));
  474. e = doc.CreateElement("", "LongHelp", "");
  475. cmd.AppendChild(e);
  476. e.AppendChild(doc.CreateTextNode(c.long_help));
  477. e = doc.CreateElement("", "Description", "");
  478. cmd.AppendChild(e);
  479. e.AppendChild(doc.CreateTextNode(c.descriptive_help));
  480. xml.AppendChild(cmd);
  481. }
  482. }
  483. }
  484. public void FromXml(XmlElement root, CommandDelegate fn)
  485. {
  486. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  487. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  488. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  489. tree.Remove("help");
  490. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  491. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  492. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  493. tree.Remove("quit");
  494. tree.Clear();
  495. ReadTreeLevel(tree, root, fn);
  496. if (!tree.ContainsKey("help"))
  497. tree["help"] = (object) new Dictionary<string, object>();
  498. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  499. if (!tree.ContainsKey("quit"))
  500. tree["quit"] = (object) new Dictionary<string, object>();
  501. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  502. }
  503. private void ReadTreeLevel(Dictionary<string, object> level, XmlNode node, CommandDelegate fn)
  504. {
  505. Dictionary<string, object> next;
  506. string name;
  507. XmlNodeList nodeL = node.ChildNodes;
  508. XmlNodeList cmdL;
  509. CommandInfo c;
  510. foreach (XmlNode part in nodeL)
  511. {
  512. switch (part.Name)
  513. {
  514. case "Level":
  515. name = ((XmlElement)part).GetAttribute("Name");
  516. next = new Dictionary<string, object>();
  517. level[name] = next;
  518. ReadTreeLevel(next, part, fn);
  519. break;
  520. case "Command":
  521. cmdL = part.ChildNodes;
  522. c = new CommandInfo();
  523. foreach (XmlNode cmdPart in cmdL)
  524. {
  525. switch (cmdPart.Name)
  526. {
  527. case "Module":
  528. c.module = cmdPart.InnerText;
  529. break;
  530. case "Shared":
  531. c.shared = Convert.ToBoolean(cmdPart.InnerText);
  532. break;
  533. case "HelpText":
  534. c.help_text = cmdPart.InnerText;
  535. break;
  536. case "LongHelp":
  537. c.long_help = cmdPart.InnerText;
  538. break;
  539. case "Description":
  540. c.descriptive_help = cmdPart.InnerText;
  541. break;
  542. }
  543. }
  544. c.fn = new List<CommandDelegate>();
  545. c.fn.Add(fn);
  546. level[String.Empty] = c;
  547. break;
  548. }
  549. }
  550. }
  551. }
  552. public class Parser
  553. {
  554. // If an unquoted portion ends with an element matching this regex
  555. // and the next element contains a space, then we have stripped
  556. // embedded quotes that should not have been stripped
  557. private static Regex optionRegex = new Regex("^--[a-zA-Z0-9-]+=$");
  558. public static string[] Parse(string text)
  559. {
  560. List<string> result = new List<string>();
  561. int index;
  562. string[] unquoted = text.Split(new char[] {'"'});
  563. for (index = 0 ; index < unquoted.Length ; index++)
  564. {
  565. if (index % 2 == 0)
  566. {
  567. string[] words = unquoted[index].Split(new char[] {' '});
  568. bool option = false;
  569. foreach (string w in words)
  570. {
  571. if (w != String.Empty)
  572. {
  573. if (optionRegex.Match(w) == Match.Empty)
  574. option = false;
  575. else
  576. option = true;
  577. result.Add(w);
  578. }
  579. }
  580. // The last item matched the regex, put the quotes back
  581. if (option)
  582. {
  583. // If the line ended with it, don't do anything
  584. if (index < (unquoted.Length - 1))
  585. {
  586. // Get and remove the option name
  587. string optionText = result[result.Count - 1];
  588. result.RemoveAt(result.Count - 1);
  589. // Add the quoted value back
  590. optionText += "\"" + unquoted[index + 1] + "\"";
  591. // Push the result into our return array
  592. result.Add(optionText);
  593. // Skip the already used value
  594. index++;
  595. }
  596. }
  597. }
  598. else
  599. {
  600. result.Add(unquoted[index]);
  601. }
  602. }
  603. return result.ToArray();
  604. }
  605. }
  606. /// <summary>
  607. /// A console that processes commands internally
  608. /// </summary>
  609. public class CommandConsole : ConsoleBase, ICommandConsole
  610. {
  611. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  612. public event OnOutputDelegate OnOutput;
  613. public ICommands Commands { get; private set; }
  614. public CommandConsole(string defaultPrompt) : base(defaultPrompt)
  615. {
  616. Commands = new Commands();
  617. Commands.AddCommand(
  618. "Help", false, "help", "help [<item>]",
  619. "Display help on a particular command or on a list of commands in a category", Help);
  620. }
  621. private void Help(string module, string[] cmd)
  622. {
  623. List<string> help = Commands.GetHelp(cmd);
  624. foreach (string s in help)
  625. Output(s);
  626. }
  627. protected void FireOnOutput(string text)
  628. {
  629. OnOutputDelegate onOutput = OnOutput;
  630. if (onOutput != null)
  631. onOutput(text);
  632. }
  633. /// <summary>
  634. /// Display a command prompt on the console and wait for user input
  635. /// </summary>
  636. public void Prompt()
  637. {
  638. string line = ReadLine(DefaultPrompt + "# ", true, true);
  639. if (line != String.Empty)
  640. Output("Invalid command");
  641. }
  642. public void RunCommand(string cmd)
  643. {
  644. string[] parts = Parser.Parse(cmd);
  645. Commands.Resolve(parts);
  646. }
  647. public override string ReadLine(string p, bool isCommand, bool e)
  648. {
  649. System.Console.Write("{0}", p);
  650. string cmdinput = System.Console.ReadLine();
  651. if (isCommand)
  652. {
  653. string[] cmd = Commands.Resolve(Parser.Parse(cmdinput));
  654. if (cmd.Length != 0)
  655. {
  656. int i;
  657. for (i=0 ; i < cmd.Length ; i++)
  658. {
  659. if (cmd[i].Contains(" "))
  660. cmd[i] = "\"" + cmd[i] + "\"";
  661. }
  662. return String.Empty;
  663. }
  664. }
  665. return cmdinput;
  666. }
  667. }
  668. }