CommandConsole.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. namespace OpenSim.Framework.Console
  36. {
  37. public delegate void CommandDelegate(string module, string[] cmd);
  38. public class Commands
  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. help.Add(commandInfo.descriptive_help);
  125. }
  126. else
  127. {
  128. help.Add(string.Format("No help is available for {0}", originalHelpRequest));
  129. }
  130. return help;
  131. }
  132. private List<string> CollectHelp(Dictionary<string, object> dict)
  133. {
  134. List<string> result = new List<string>();
  135. foreach (KeyValuePair<string, object> kvp in dict)
  136. {
  137. if (kvp.Value is Dictionary<string, Object>)
  138. {
  139. result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value));
  140. }
  141. else
  142. {
  143. if (((CommandInfo)kvp.Value).long_help != String.Empty)
  144. result.Add(((CommandInfo)kvp.Value).help_text+" - "+
  145. ((CommandInfo)kvp.Value).long_help);
  146. }
  147. }
  148. return result;
  149. }
  150. /// <summary>
  151. /// Add a command to those which can be invoked from the console.
  152. /// </summary>
  153. /// <param name="module"></param>
  154. /// <param name="command"></param>
  155. /// <param name="help"></param>
  156. /// <param name="longhelp"></param>
  157. /// <param name="fn"></param>
  158. public void AddCommand(string module, bool shared, string command,
  159. string help, string longhelp, CommandDelegate fn)
  160. {
  161. AddCommand(module, shared, command, help, longhelp,
  162. String.Empty, fn);
  163. }
  164. /// <summary>
  165. /// Add a command to those which can be invoked from the console.
  166. /// </summary>
  167. /// <param name="module"></param>
  168. /// <param name="command"></param>
  169. /// <param name="help"></param>
  170. /// <param name="longhelp"></param>
  171. /// <param name="descriptivehelp"></param>
  172. /// <param name="fn"></param>
  173. public void AddCommand(string module, bool shared, string command,
  174. string help, string longhelp, string descriptivehelp,
  175. CommandDelegate fn)
  176. {
  177. string[] parts = Parser.Parse(command);
  178. Dictionary<string, Object> current = tree;
  179. foreach (string s in parts)
  180. {
  181. if (current.ContainsKey(s))
  182. {
  183. if (current[s] is Dictionary<string, Object>)
  184. {
  185. current = (Dictionary<string, Object>)current[s];
  186. }
  187. else
  188. return;
  189. }
  190. else
  191. {
  192. current[s] = new Dictionary<string, Object>();
  193. current = (Dictionary<string, Object>)current[s];
  194. }
  195. }
  196. CommandInfo info;
  197. if (current.ContainsKey(String.Empty))
  198. {
  199. info = (CommandInfo)current[String.Empty];
  200. if (!info.shared && !info.fn.Contains(fn))
  201. info.fn.Add(fn);
  202. return;
  203. }
  204. info = new CommandInfo();
  205. info.module = module;
  206. info.shared = shared;
  207. info.help_text = help;
  208. info.long_help = longhelp;
  209. info.descriptive_help = descriptivehelp;
  210. info.fn = new List<CommandDelegate>();
  211. info.fn.Add(fn);
  212. current[String.Empty] = info;
  213. }
  214. public string[] FindNextOption(string[] cmd, bool term)
  215. {
  216. Dictionary<string, object> current = tree;
  217. int remaining = cmd.Length;
  218. foreach (string s in cmd)
  219. {
  220. remaining--;
  221. List<string> found = new List<string>();
  222. foreach (string opt in current.Keys)
  223. {
  224. if (remaining > 0 && opt == s)
  225. {
  226. found.Clear();
  227. found.Add(opt);
  228. break;
  229. }
  230. if (opt.StartsWith(s))
  231. {
  232. found.Add(opt);
  233. }
  234. }
  235. if (found.Count == 1 && (remaining != 0 || term))
  236. {
  237. current = (Dictionary<string, object>)current[found[0]];
  238. }
  239. else if (found.Count > 0)
  240. {
  241. return found.ToArray();
  242. }
  243. else
  244. {
  245. break;
  246. // return new string[] {"<cr>"};
  247. }
  248. }
  249. if (current.Count > 1)
  250. {
  251. List<string> choices = new List<string>();
  252. bool addcr = false;
  253. foreach (string s in current.Keys)
  254. {
  255. if (s == String.Empty)
  256. {
  257. CommandInfo ci = (CommandInfo)current[String.Empty];
  258. if (ci.fn.Count != 0)
  259. addcr = true;
  260. }
  261. else
  262. choices.Add(s);
  263. }
  264. if (addcr)
  265. choices.Add("<cr>");
  266. return choices.ToArray();
  267. }
  268. if (current.ContainsKey(String.Empty))
  269. return new string[] { "Command help: "+((CommandInfo)current[String.Empty]).help_text};
  270. return new string[] { new List<string>(current.Keys)[0] };
  271. }
  272. public string[] Resolve(string[] cmd)
  273. {
  274. string[] result = cmd;
  275. int index = -1;
  276. Dictionary<string, object> current = tree;
  277. foreach (string s in cmd)
  278. {
  279. index++;
  280. List<string> found = new List<string>();
  281. foreach (string opt in current.Keys)
  282. {
  283. if (opt == s)
  284. {
  285. found.Clear();
  286. found.Add(opt);
  287. break;
  288. }
  289. if (opt.StartsWith(s))
  290. {
  291. found.Add(opt);
  292. }
  293. }
  294. if (found.Count == 1)
  295. {
  296. result[index] = found[0];
  297. current = (Dictionary<string, object>)current[found[0]];
  298. }
  299. else if (found.Count > 0)
  300. {
  301. return new string[0];
  302. }
  303. else
  304. {
  305. break;
  306. }
  307. }
  308. if (current.ContainsKey(String.Empty))
  309. {
  310. CommandInfo ci = (CommandInfo)current[String.Empty];
  311. if (ci.fn.Count == 0)
  312. return new string[0];
  313. foreach (CommandDelegate fn in ci.fn)
  314. {
  315. if (fn != null)
  316. fn(ci.module, result);
  317. else
  318. return new string[0];
  319. }
  320. return result;
  321. }
  322. return new string[0];
  323. }
  324. public XmlElement GetXml(XmlDocument doc)
  325. {
  326. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  327. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  328. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  329. tree.Remove("help");
  330. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  331. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  332. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  333. tree.Remove("quit");
  334. XmlElement root = doc.CreateElement("", "HelpTree", "");
  335. ProcessTreeLevel(tree, root, doc);
  336. if (!tree.ContainsKey("help"))
  337. tree["help"] = (object) new Dictionary<string, object>();
  338. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  339. if (!tree.ContainsKey("quit"))
  340. tree["quit"] = (object) new Dictionary<string, object>();
  341. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  342. return root;
  343. }
  344. private void ProcessTreeLevel(Dictionary<string, object> level, XmlElement xml, XmlDocument doc)
  345. {
  346. foreach (KeyValuePair<string, object> kvp in level)
  347. {
  348. if (kvp.Value is Dictionary<string, Object>)
  349. {
  350. XmlElement next = doc.CreateElement("", "Level", "");
  351. next.SetAttribute("Name", kvp.Key);
  352. xml.AppendChild(next);
  353. ProcessTreeLevel((Dictionary<string, object>)kvp.Value, next, doc);
  354. }
  355. else
  356. {
  357. CommandInfo c = (CommandInfo)kvp.Value;
  358. XmlElement cmd = doc.CreateElement("", "Command", "");
  359. XmlElement e;
  360. e = doc.CreateElement("", "Module", "");
  361. cmd.AppendChild(e);
  362. e.AppendChild(doc.CreateTextNode(c.module));
  363. e = doc.CreateElement("", "Shared", "");
  364. cmd.AppendChild(e);
  365. e.AppendChild(doc.CreateTextNode(c.shared.ToString()));
  366. e = doc.CreateElement("", "HelpText", "");
  367. cmd.AppendChild(e);
  368. e.AppendChild(doc.CreateTextNode(c.help_text));
  369. e = doc.CreateElement("", "LongHelp", "");
  370. cmd.AppendChild(e);
  371. e.AppendChild(doc.CreateTextNode(c.long_help));
  372. e = doc.CreateElement("", "Description", "");
  373. cmd.AppendChild(e);
  374. e.AppendChild(doc.CreateTextNode(c.descriptive_help));
  375. xml.AppendChild(cmd);
  376. }
  377. }
  378. }
  379. public void FromXml(XmlElement root, CommandDelegate fn)
  380. {
  381. CommandInfo help = (CommandInfo)((Dictionary<string, object>)tree["help"])[String.Empty];
  382. ((Dictionary<string, object>)tree["help"]).Remove(string.Empty);
  383. if (((Dictionary<string, object>)tree["help"]).Count == 0)
  384. tree.Remove("help");
  385. CommandInfo quit = (CommandInfo)((Dictionary<string, object>)tree["quit"])[String.Empty];
  386. ((Dictionary<string, object>)tree["quit"]).Remove(string.Empty);
  387. if (((Dictionary<string, object>)tree["quit"]).Count == 0)
  388. tree.Remove("quit");
  389. tree.Clear();
  390. ReadTreeLevel(tree, root, fn);
  391. if (!tree.ContainsKey("help"))
  392. tree["help"] = (object) new Dictionary<string, object>();
  393. ((Dictionary<string, object>)tree["help"])[String.Empty] = help;
  394. if (!tree.ContainsKey("quit"))
  395. tree["quit"] = (object) new Dictionary<string, object>();
  396. ((Dictionary<string, object>)tree["quit"])[String.Empty] = quit;
  397. }
  398. private void ReadTreeLevel(Dictionary<string, object> level, XmlNode node, CommandDelegate fn)
  399. {
  400. Dictionary<string, object> next;
  401. string name;
  402. XmlNodeList nodeL = node.ChildNodes;
  403. XmlNodeList cmdL;
  404. CommandInfo c;
  405. foreach (XmlNode part in nodeL)
  406. {
  407. switch (part.Name)
  408. {
  409. case "Level":
  410. name = ((XmlElement)part).GetAttribute("Name");
  411. next = new Dictionary<string, object>();
  412. level[name] = next;
  413. ReadTreeLevel(next, part, fn);
  414. break;
  415. case "Command":
  416. cmdL = part.ChildNodes;
  417. c = new CommandInfo();
  418. foreach (XmlNode cmdPart in cmdL)
  419. {
  420. switch (cmdPart.Name)
  421. {
  422. case "Module":
  423. c.module = cmdPart.InnerText;
  424. break;
  425. case "Shared":
  426. c.shared = Convert.ToBoolean(cmdPart.InnerText);
  427. break;
  428. case "HelpText":
  429. c.help_text = cmdPart.InnerText;
  430. break;
  431. case "LongHelp":
  432. c.long_help = cmdPart.InnerText;
  433. break;
  434. case "Description":
  435. c.descriptive_help = cmdPart.InnerText;
  436. break;
  437. }
  438. }
  439. c.fn = new List<CommandDelegate>();
  440. c.fn.Add(fn);
  441. level[String.Empty] = c;
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. public class Parser
  448. {
  449. public static string[] Parse(string text)
  450. {
  451. List<string> result = new List<string>();
  452. int index;
  453. string[] unquoted = text.Split(new char[] {'"'});
  454. for (index = 0 ; index < unquoted.Length ; index++)
  455. {
  456. if (index % 2 == 0)
  457. {
  458. string[] words = unquoted[index].Split(new char[] {' '});
  459. foreach (string w in words)
  460. {
  461. if (w != String.Empty)
  462. result.Add(w);
  463. }
  464. }
  465. else
  466. {
  467. result.Add(unquoted[index]);
  468. }
  469. }
  470. return result.ToArray();
  471. }
  472. }
  473. // A console that processes commands internally
  474. //
  475. public class CommandConsole : ConsoleBase
  476. {
  477. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  478. public Commands Commands = new Commands();
  479. public CommandConsole(string defaultPrompt) : base(defaultPrompt)
  480. {
  481. Commands.AddCommand("console", false, "help", "help [<command>]",
  482. "Get general command list or more detailed help on a specific command", Help);
  483. }
  484. private void Help(string module, string[] cmd)
  485. {
  486. List<string> help = Commands.GetHelp(cmd);
  487. foreach (string s in help)
  488. Output(s);
  489. }
  490. public void Prompt()
  491. {
  492. string line = ReadLine(m_defaultPrompt, true, true);
  493. if (line != String.Empty)
  494. {
  495. m_log.Info("[CONSOLE] Invalid command");
  496. }
  497. }
  498. public void RunCommand(string cmd)
  499. {
  500. string[] parts = Parser.Parse(cmd);
  501. Commands.Resolve(parts);
  502. }
  503. public override string ReadLine(string p, bool isCommand, bool e)
  504. {
  505. System.Console.Write("{0}", prompt);
  506. string cmdinput = System.Console.ReadLine();
  507. if (isCommand)
  508. {
  509. string[] cmd = Commands.Resolve(Parser.Parse(cmdinput));
  510. if (cmd.Length != 0)
  511. {
  512. int i;
  513. for (i=0 ; i < cmd.Length ; i++)
  514. {
  515. if (cmd[i].Contains(" "))
  516. cmd[i] = "\"" + cmd[i] + "\"";
  517. }
  518. return String.Empty;
  519. }
  520. }
  521. return cmdinput;
  522. }
  523. }
  524. }