CommandConsole.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. public string[] Resolve(string[] cmd)
  368. {
  369. string[] 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 new string[0];
  397. }
  398. else
  399. {
  400. break;
  401. }
  402. }
  403. if (current.ContainsKey(String.Empty))
  404. {
  405. CommandInfo ci = (CommandInfo)current[String.Empty];
  406. if (ci.fn.Count == 0)
  407. return new string[0];
  408. foreach (CommandDelegate fn in ci.fn)
  409. {
  410. if (fn != null)
  411. fn(ci.module, result);
  412. else
  413. return new string[0];
  414. }
  415. return result;
  416. }
  417. return new string[0];
  418. }
  419. public XmlElement GetXml(XmlDocument doc)
  420. {
  421. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  422. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  423. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  424. tree.Remove("help");
  425. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  426. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  427. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  428. tree.Remove("quit");
  429. XmlElement root = doc.CreateElement("", "HelpTree", "");
  430. ProcessTreeLevel(tree, root, doc);
  431. if (!tree.ContainsKey("help"))
  432. tree["help"] = (object) new Dictionary<string, object>();
  433. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  434. if (!tree.ContainsKey("quit"))
  435. tree["quit"] = (object) new Dictionary<string, object>();
  436. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  437. return root;
  438. }
  439. private void ProcessTreeLevel(Dictionary<string, object> level, XmlElement xml, XmlDocument doc)
  440. {
  441. foreach (KeyValuePair<string, object> kvp in level)
  442. {
  443. if (kvp.Value is Dictionary<string, Object>)
  444. {
  445. XmlElement next = doc.CreateElement("", "Level", "");
  446. next.SetAttribute("Name", kvp.Key);
  447. xml.AppendChild(next);
  448. ProcessTreeLevel((Dictionary<string, object>)kvp.Value, next, doc);
  449. }
  450. else
  451. {
  452. CommandInfo c = (CommandInfo)kvp.Value;
  453. XmlElement cmd = doc.CreateElement("", "Command", "");
  454. XmlElement e;
  455. e = doc.CreateElement("", "Module", "");
  456. cmd.AppendChild(e);
  457. e.AppendChild(doc.CreateTextNode(c.module));
  458. e = doc.CreateElement("", "Shared", "");
  459. cmd.AppendChild(e);
  460. e.AppendChild(doc.CreateTextNode(c.shared.ToString()));
  461. e = doc.CreateElement("", "HelpText", "");
  462. cmd.AppendChild(e);
  463. e.AppendChild(doc.CreateTextNode(c.help_text));
  464. e = doc.CreateElement("", "LongHelp", "");
  465. cmd.AppendChild(e);
  466. e.AppendChild(doc.CreateTextNode(c.long_help));
  467. e = doc.CreateElement("", "Description", "");
  468. cmd.AppendChild(e);
  469. e.AppendChild(doc.CreateTextNode(c.descriptive_help));
  470. xml.AppendChild(cmd);
  471. }
  472. }
  473. }
  474. public void FromXml(XmlElement root, CommandDelegate fn)
  475. {
  476. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  477. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  478. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  479. tree.Remove("help");
  480. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  481. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  482. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  483. tree.Remove("quit");
  484. tree.Clear();
  485. ReadTreeLevel(tree, root, fn);
  486. if (!tree.ContainsKey("help"))
  487. tree["help"] = (object) new Dictionary<string, object>();
  488. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  489. if (!tree.ContainsKey("quit"))
  490. tree["quit"] = (object) new Dictionary<string, object>();
  491. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  492. }
  493. private void ReadTreeLevel(Dictionary<string, object> level, XmlNode node, CommandDelegate fn)
  494. {
  495. Dictionary<string, object> next;
  496. string name;
  497. XmlNodeList nodeL = node.ChildNodes;
  498. XmlNodeList cmdL;
  499. CommandInfo c;
  500. foreach (XmlNode part in nodeL)
  501. {
  502. switch (part.Name)
  503. {
  504. case "Level":
  505. name = ((XmlElement)part).GetAttribute("Name");
  506. next = new Dictionary<string, object>();
  507. level[name] = next;
  508. ReadTreeLevel(next, part, fn);
  509. break;
  510. case "Command":
  511. cmdL = part.ChildNodes;
  512. c = new CommandInfo();
  513. foreach (XmlNode cmdPart in cmdL)
  514. {
  515. switch (cmdPart.Name)
  516. {
  517. case "Module":
  518. c.module = cmdPart.InnerText;
  519. break;
  520. case "Shared":
  521. c.shared = Convert.ToBoolean(cmdPart.InnerText);
  522. break;
  523. case "HelpText":
  524. c.help_text = cmdPart.InnerText;
  525. break;
  526. case "LongHelp":
  527. c.long_help = cmdPart.InnerText;
  528. break;
  529. case "Description":
  530. c.descriptive_help = cmdPart.InnerText;
  531. break;
  532. }
  533. }
  534. c.fn = new List<CommandDelegate>();
  535. c.fn.Add(fn);
  536. level[String.Empty] = c;
  537. break;
  538. }
  539. }
  540. }
  541. }
  542. public class Parser
  543. {
  544. // If an unquoted portion ends with an element matching this regex
  545. // and the next element contains a space, then we have stripped
  546. // embedded quotes that should not have been stripped
  547. private static Regex optionRegex = new Regex("^--[a-zA-Z0-9-]+=$");
  548. public static string[] Parse(string text)
  549. {
  550. List<string> result = new List<string>();
  551. int index;
  552. string[] unquoted = text.Split(new char[] {'"'});
  553. for (index = 0 ; index < unquoted.Length ; index++)
  554. {
  555. if (index % 2 == 0)
  556. {
  557. string[] words = unquoted[index].Split(new char[] {' '});
  558. bool option = false;
  559. foreach (string w in words)
  560. {
  561. if (w != String.Empty)
  562. {
  563. if (optionRegex.Match(w) == Match.Empty)
  564. option = false;
  565. else
  566. option = true;
  567. result.Add(w);
  568. }
  569. }
  570. // The last item matched the regex, put the quotes back
  571. if (option)
  572. {
  573. // If the line ended with it, don't do anything
  574. if (index < (unquoted.Length - 1))
  575. {
  576. // Get and remove the option name
  577. string optionText = result[result.Count - 1];
  578. result.RemoveAt(result.Count - 1);
  579. // Add the quoted value back
  580. optionText += "\"" + unquoted[index + 1] + "\"";
  581. // Push the result into our return array
  582. result.Add(optionText);
  583. // Skip the already used value
  584. index++;
  585. }
  586. }
  587. }
  588. else
  589. {
  590. result.Add(unquoted[index]);
  591. }
  592. }
  593. return result.ToArray();
  594. }
  595. }
  596. /// <summary>
  597. /// A console that processes commands internally
  598. /// </summary>
  599. public class CommandConsole : ConsoleBase, ICommandConsole
  600. {
  601. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  602. public event OnOutputDelegate OnOutput;
  603. public ICommands Commands { get; private set; }
  604. public CommandConsole(string defaultPrompt) : base(defaultPrompt)
  605. {
  606. Commands = new Commands();
  607. Commands.AddCommand(
  608. "Help", false, "help", "help [<item>]",
  609. "Display help on a particular command or on a list of commands in a category", Help);
  610. }
  611. private void Help(string module, string[] cmd)
  612. {
  613. List<string> help = Commands.GetHelp(cmd);
  614. foreach (string s in help)
  615. Output(s);
  616. }
  617. protected void FireOnOutput(string text)
  618. {
  619. OnOutputDelegate onOutput = OnOutput;
  620. if (onOutput != null)
  621. onOutput(text);
  622. }
  623. /// <summary>
  624. /// Display a command prompt on the console and wait for user input
  625. /// </summary>
  626. public void Prompt()
  627. {
  628. string line = ReadLine(DefaultPrompt + "# ", true, true);
  629. if (line != String.Empty)
  630. Output("Invalid command");
  631. }
  632. public void RunCommand(string cmd)
  633. {
  634. string[] parts = Parser.Parse(cmd);
  635. Commands.Resolve(parts);
  636. }
  637. public override string ReadLine(string p, bool isCommand, bool e)
  638. {
  639. System.Console.Write("{0}", p);
  640. string cmdinput = System.Console.ReadLine();
  641. if (isCommand)
  642. {
  643. string[] cmd = Commands.Resolve(Parser.Parse(cmdinput));
  644. if (cmd.Length != 0)
  645. {
  646. int i;
  647. for (i=0 ; i < cmd.Length ; i++)
  648. {
  649. if (cmd[i].Contains(" "))
  650. cmd[i] = "\"" + cmd[i] + "\"";
  651. }
  652. return String.Empty;
  653. }
  654. }
  655. return cmdinput;
  656. }
  657. }
  658. }