CommandConsole.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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.Reflection;
  32. using System.Text;
  33. using System.Threading;
  34. using log4net;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Framework.Console
  37. {
  38. public class Commands : ICommands
  39. {
  40. /// <summary>
  41. /// Encapsulates a command that can be invoked from the console
  42. /// </summary>
  43. private class CommandInfo
  44. {
  45. /// <value>
  46. /// The module from which this command comes
  47. /// </value>
  48. public string module;
  49. /// <value>
  50. /// Whether the module is shared
  51. /// </value>
  52. public bool shared;
  53. /// <value>
  54. /// Very short BNF description
  55. /// </value>
  56. public string help_text;
  57. /// <value>
  58. /// Longer one line help text
  59. /// </value>
  60. public string long_help;
  61. /// <value>
  62. /// Full descriptive help for this command
  63. /// </value>
  64. public string descriptive_help;
  65. /// <value>
  66. /// The method to invoke for this command
  67. /// </value>
  68. public List<CommandDelegate> fn;
  69. }
  70. /// <value>
  71. /// Commands organized by keyword in a tree
  72. /// </value>
  73. private Dictionary<string, object> tree =
  74. new Dictionary<string, object>();
  75. /// <summary>
  76. /// Get help for the given help string
  77. /// </summary>
  78. /// <param name="helpParts">Parsed parts of the help string. If empty then general help is returned.</param>
  79. /// <returns></returns>
  80. public List<string> GetHelp(string[] cmd)
  81. {
  82. List<string> help = new List<string>();
  83. List<string> helpParts = new List<string>(cmd);
  84. // Remove initial help keyword
  85. helpParts.RemoveAt(0);
  86. // General help
  87. if (helpParts.Count == 0)
  88. {
  89. help.AddRange(CollectHelp(tree));
  90. help.Sort();
  91. }
  92. else
  93. {
  94. help.AddRange(CollectHelp(helpParts));
  95. }
  96. return help;
  97. }
  98. /// <summary>
  99. /// See if we can find the requested command in order to display longer help
  100. /// </summary>
  101. /// <param name="helpParts"></param>
  102. /// <returns></returns>
  103. private List<string> CollectHelp(List<string> helpParts)
  104. {
  105. string originalHelpRequest = string.Join(" ", helpParts.ToArray());
  106. List<string> help = new List<string>();
  107. Dictionary<string, object> dict = tree;
  108. while (helpParts.Count > 0)
  109. {
  110. string helpPart = helpParts[0];
  111. if (!dict.ContainsKey(helpPart))
  112. break;
  113. //m_log.Debug("Found {0}", helpParts[0]);
  114. if (dict[helpPart] is Dictionary<string, Object>)
  115. dict = (Dictionary<string, object>)dict[helpPart];
  116. helpParts.RemoveAt(0);
  117. }
  118. // There was a command for the given help string
  119. if (dict.ContainsKey(String.Empty))
  120. {
  121. CommandInfo commandInfo = (CommandInfo)dict[String.Empty];
  122. help.Add(commandInfo.help_text);
  123. help.Add(commandInfo.long_help);
  124. string descriptiveHelp = commandInfo.descriptive_help;
  125. // If we do have some descriptive help then insert a spacing line before and after for readability.
  126. if (descriptiveHelp != string.Empty)
  127. help.Add(string.Empty);
  128. help.Add(commandInfo.descriptive_help);
  129. if (descriptiveHelp != string.Empty)
  130. help.Add(string.Empty);
  131. }
  132. else
  133. {
  134. help.Add(string.Format("No help is available for {0}", originalHelpRequest));
  135. }
  136. return help;
  137. }
  138. private List<string> CollectHelp(Dictionary<string, object> dict)
  139. {
  140. List<string> result = new List<string>();
  141. foreach (KeyValuePair<string, object> kvp in dict)
  142. {
  143. if (kvp.Value is Dictionary<string, Object>)
  144. {
  145. result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value));
  146. }
  147. else
  148. {
  149. if (((CommandInfo)kvp.Value).long_help != String.Empty)
  150. result.Add(((CommandInfo)kvp.Value).help_text+" - "+
  151. ((CommandInfo)kvp.Value).long_help);
  152. }
  153. }
  154. return result;
  155. }
  156. /// <summary>
  157. /// Add a command to those which can be invoked from the console.
  158. /// </summary>
  159. /// <param name="module"></param>
  160. /// <param name="command"></param>
  161. /// <param name="help"></param>
  162. /// <param name="longhelp"></param>
  163. /// <param name="fn"></param>
  164. public void AddCommand(string module, bool shared, string command,
  165. string help, string longhelp, CommandDelegate fn)
  166. {
  167. AddCommand(module, shared, command, help, longhelp, String.Empty, fn);
  168. }
  169. /// <summary>
  170. /// Add a command to those which can be invoked from the console.
  171. /// </summary>
  172. /// <param name="module"></param>
  173. /// <param name="command"></param>
  174. /// <param name="help"></param>
  175. /// <param name="longhelp"></param>
  176. /// <param name="descriptivehelp"></param>
  177. /// <param name="fn"></param>
  178. public void AddCommand(string module, bool shared, string command,
  179. string help, string longhelp, string descriptivehelp,
  180. CommandDelegate fn)
  181. {
  182. string[] parts = Parser.Parse(command);
  183. Dictionary<string, Object> current = tree;
  184. foreach (string s in parts)
  185. {
  186. if (current.ContainsKey(s))
  187. {
  188. if (current[s] is Dictionary<string, Object>)
  189. {
  190. current = (Dictionary<string, Object>)current[s];
  191. }
  192. else
  193. return;
  194. }
  195. else
  196. {
  197. current[s] = new Dictionary<string, Object>();
  198. current = (Dictionary<string, Object>)current[s];
  199. }
  200. }
  201. CommandInfo info;
  202. if (current.ContainsKey(String.Empty))
  203. {
  204. info = (CommandInfo)current[String.Empty];
  205. if (!info.shared && !info.fn.Contains(fn))
  206. info.fn.Add(fn);
  207. return;
  208. }
  209. info = new CommandInfo();
  210. info.module = module;
  211. info.shared = shared;
  212. info.help_text = help;
  213. info.long_help = longhelp;
  214. info.descriptive_help = descriptivehelp;
  215. info.fn = new List<CommandDelegate>();
  216. info.fn.Add(fn);
  217. current[String.Empty] = info;
  218. }
  219. public string[] FindNextOption(string[] cmd, bool term)
  220. {
  221. Dictionary<string, object> current = tree;
  222. int remaining = cmd.Length;
  223. foreach (string s in cmd)
  224. {
  225. remaining--;
  226. List<string> found = new List<string>();
  227. foreach (string opt in current.Keys)
  228. {
  229. if (remaining > 0 && opt == s)
  230. {
  231. found.Clear();
  232. found.Add(opt);
  233. break;
  234. }
  235. if (opt.StartsWith(s))
  236. {
  237. found.Add(opt);
  238. }
  239. }
  240. if (found.Count == 1 && (remaining != 0 || term))
  241. {
  242. current = (Dictionary<string, object>)current[found[0]];
  243. }
  244. else if (found.Count > 0)
  245. {
  246. return found.ToArray();
  247. }
  248. else
  249. {
  250. break;
  251. // return new string[] {"<cr>"};
  252. }
  253. }
  254. if (current.Count > 1)
  255. {
  256. List<string> choices = new List<string>();
  257. bool addcr = false;
  258. foreach (string s in current.Keys)
  259. {
  260. if (s == String.Empty)
  261. {
  262. CommandInfo ci = (CommandInfo)current[String.Empty];
  263. if (ci.fn.Count != 0)
  264. addcr = true;
  265. }
  266. else
  267. choices.Add(s);
  268. }
  269. if (addcr)
  270. choices.Add("<cr>");
  271. return choices.ToArray();
  272. }
  273. if (current.ContainsKey(String.Empty))
  274. return new string[] { "Command help: "+((CommandInfo)current[String.Empty]).help_text};
  275. return new string[] { new List<string>(current.Keys)[0] };
  276. }
  277. public string[] Resolve(string[] cmd)
  278. {
  279. string[] result = cmd;
  280. int index = -1;
  281. Dictionary<string, object> current = tree;
  282. foreach (string s in cmd)
  283. {
  284. index++;
  285. List<string> found = new List<string>();
  286. foreach (string opt in current.Keys)
  287. {
  288. if (opt == s)
  289. {
  290. found.Clear();
  291. found.Add(opt);
  292. break;
  293. }
  294. if (opt.StartsWith(s))
  295. {
  296. found.Add(opt);
  297. }
  298. }
  299. if (found.Count == 1)
  300. {
  301. result[index] = found[0];
  302. current = (Dictionary<string, object>)current[found[0]];
  303. }
  304. else if (found.Count > 0)
  305. {
  306. return new string[0];
  307. }
  308. else
  309. {
  310. break;
  311. }
  312. }
  313. if (current.ContainsKey(String.Empty))
  314. {
  315. CommandInfo ci = (CommandInfo)current[String.Empty];
  316. if (ci.fn.Count == 0)
  317. return new string[0];
  318. foreach (CommandDelegate fn in ci.fn)
  319. {
  320. if (fn != null)
  321. fn(ci.module, result);
  322. else
  323. return new string[0];
  324. }
  325. return result;
  326. }
  327. return new string[0];
  328. }
  329. public XmlElement GetXml(XmlDocument doc)
  330. {
  331. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  332. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  333. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  334. tree.Remove("help");
  335. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  336. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  337. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  338. tree.Remove("quit");
  339. XmlElement root = doc.CreateElement("", "HelpTree", "");
  340. ProcessTreeLevel(tree, root, doc);
  341. if (!tree.ContainsKey("help"))
  342. tree["help"] = (object) new Dictionary<string, object>();
  343. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  344. if (!tree.ContainsKey("quit"))
  345. tree["quit"] = (object) new Dictionary<string, object>();
  346. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  347. return root;
  348. }
  349. private void ProcessTreeLevel(Dictionary<string, object> level, XmlElement xml, XmlDocument doc)
  350. {
  351. foreach (KeyValuePair<string, object> kvp in level)
  352. {
  353. if (kvp.Value is Dictionary<string, Object>)
  354. {
  355. XmlElement next = doc.CreateElement("", "Level", "");
  356. next.SetAttribute("Name", kvp.Key);
  357. xml.AppendChild(next);
  358. ProcessTreeLevel((Dictionary<string, object>)kvp.Value, next, doc);
  359. }
  360. else
  361. {
  362. CommandInfo c = (CommandInfo)kvp.Value;
  363. XmlElement cmd = doc.CreateElement("", "Command", "");
  364. XmlElement e;
  365. e = doc.CreateElement("", "Module", "");
  366. cmd.AppendChild(e);
  367. e.AppendChild(doc.CreateTextNode(c.module));
  368. e = doc.CreateElement("", "Shared", "");
  369. cmd.AppendChild(e);
  370. e.AppendChild(doc.CreateTextNode(c.shared.ToString()));
  371. e = doc.CreateElement("", "HelpText", "");
  372. cmd.AppendChild(e);
  373. e.AppendChild(doc.CreateTextNode(c.help_text));
  374. e = doc.CreateElement("", "LongHelp", "");
  375. cmd.AppendChild(e);
  376. e.AppendChild(doc.CreateTextNode(c.long_help));
  377. e = doc.CreateElement("", "Description", "");
  378. cmd.AppendChild(e);
  379. e.AppendChild(doc.CreateTextNode(c.descriptive_help));
  380. xml.AppendChild(cmd);
  381. }
  382. }
  383. }
  384. public void FromXml(XmlElement root, CommandDelegate fn)
  385. {
  386. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  387. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  388. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  389. tree.Remove("help");
  390. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  391. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  392. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  393. tree.Remove("quit");
  394. tree.Clear();
  395. ReadTreeLevel(tree, root, fn);
  396. if (!tree.ContainsKey("help"))
  397. tree["help"] = (object) new Dictionary<string, object>();
  398. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  399. if (!tree.ContainsKey("quit"))
  400. tree["quit"] = (object) new Dictionary<string, object>();
  401. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  402. }
  403. private void ReadTreeLevel(Dictionary<string, object> level, XmlNode node, CommandDelegate fn)
  404. {
  405. Dictionary<string, object> next;
  406. string name;
  407. XmlNodeList nodeL = node.ChildNodes;
  408. XmlNodeList cmdL;
  409. CommandInfo c;
  410. foreach (XmlNode part in nodeL)
  411. {
  412. switch (part.Name)
  413. {
  414. case "Level":
  415. name = ((XmlElement)part).GetAttribute("Name");
  416. next = new Dictionary<string, object>();
  417. level[name] = next;
  418. ReadTreeLevel(next, part, fn);
  419. break;
  420. case "Command":
  421. cmdL = part.ChildNodes;
  422. c = new CommandInfo();
  423. foreach (XmlNode cmdPart in cmdL)
  424. {
  425. switch (cmdPart.Name)
  426. {
  427. case "Module":
  428. c.module = cmdPart.InnerText;
  429. break;
  430. case "Shared":
  431. c.shared = Convert.ToBoolean(cmdPart.InnerText);
  432. break;
  433. case "HelpText":
  434. c.help_text = cmdPart.InnerText;
  435. break;
  436. case "LongHelp":
  437. c.long_help = cmdPart.InnerText;
  438. break;
  439. case "Description":
  440. c.descriptive_help = cmdPart.InnerText;
  441. break;
  442. }
  443. }
  444. c.fn = new List<CommandDelegate>();
  445. c.fn.Add(fn);
  446. level[String.Empty] = c;
  447. break;
  448. }
  449. }
  450. }
  451. }
  452. public class Parser
  453. {
  454. public static string[] Parse(string text)
  455. {
  456. List<string> result = new List<string>();
  457. int index;
  458. string[] unquoted = text.Split(new char[] {'"'});
  459. for (index = 0 ; index < unquoted.Length ; index++)
  460. {
  461. if (index % 2 == 0)
  462. {
  463. string[] words = unquoted[index].Split(new char[] {' '});
  464. foreach (string w in words)
  465. {
  466. if (w != String.Empty)
  467. result.Add(w);
  468. }
  469. }
  470. else
  471. {
  472. result.Add(unquoted[index]);
  473. }
  474. }
  475. return result.ToArray();
  476. }
  477. }
  478. /// <summary>
  479. /// A console that processes commands internally
  480. /// </summary>
  481. public class CommandConsole : ConsoleBase, ICommandConsole
  482. {
  483. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  484. public ICommands Commands { get; private set; }
  485. public CommandConsole(string defaultPrompt) : base(defaultPrompt)
  486. {
  487. Commands = new Commands();
  488. Commands.AddCommand("console", false, "help", "help [<command>]",
  489. "Get general command list or more detailed help on a specific command", Help);
  490. }
  491. private void Help(string module, string[] cmd)
  492. {
  493. List<string> help = Commands.GetHelp(cmd);
  494. foreach (string s in help)
  495. Output(s);
  496. }
  497. /// <summary>
  498. /// Display a command prompt on the console and wait for user input
  499. /// </summary>
  500. public void Prompt()
  501. {
  502. string line = ReadLine(m_defaultPrompt + "# ", true, true);
  503. if (line != String.Empty)
  504. Output("Invalid command");
  505. }
  506. public void RunCommand(string cmd)
  507. {
  508. string[] parts = Parser.Parse(cmd);
  509. Commands.Resolve(parts);
  510. }
  511. public override string ReadLine(string p, bool isCommand, bool e)
  512. {
  513. System.Console.Write("{0}", p);
  514. string cmdinput = System.Console.ReadLine();
  515. if (isCommand)
  516. {
  517. string[] cmd = Commands.Resolve(Parser.Parse(cmdinput));
  518. if (cmd.Length != 0)
  519. {
  520. int i;
  521. for (i=0 ; i < cmd.Length ; i++)
  522. {
  523. if (cmd[i].Contains(" "))
  524. cmd[i] = "\"" + cmd[i] + "\"";
  525. }
  526. return String.Empty;
  527. }
  528. }
  529. return cmdinput;
  530. }
  531. }
  532. }