Console.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // version 2 as published by the Free Software Foundation.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License version 2
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  27. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  28. //
  29. //
  30. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  31. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  32. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  33. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  34. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  35. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  36. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  37. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  38. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  39. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  40. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  41. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  42. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  43. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  44. //
  45. // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  46. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  47. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  48. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  49. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  50. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  51. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  52. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  53. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  54. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  55. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  56. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  57. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  58. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  59. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  60. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  61. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  62. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  63. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  64. //
  65. //
  66. // SOURCE CODE CONTRIBUTION
  67. // ------------------------
  68. //
  69. // Your contribution to SoftEther VPN Project is much appreciated.
  70. // Please send patches to us through GitHub.
  71. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  72. // http://www.softether.org/5-download/src/9.patch
  73. //
  74. //
  75. // DEAR SECURITY EXPERTS
  76. // ---------------------
  77. //
  78. // If you find a bug or a security vulnerability please kindly inform us
  79. // about the problem immediately so that we can fix the security problem
  80. // to protect a lot of users around the world as soon as possible.
  81. //
  82. // Our e-mail address for security reports is:
  83. // softether-vpn-security [at] softether.org
  84. //
  85. // Please note that the above e-mail address is not a technical support
  86. // inquiry address. If you need technical assistance, please visit
  87. // http://www.softether.org/ and ask your question on the users forum.
  88. //
  89. // Thank you for your cooperation.
  90. //
  91. //
  92. // NO MEMORY OR RESOURCE LEAKS
  93. // ---------------------------
  94. //
  95. // The memory-leaks and resource-leaks verification under the stress
  96. // test has been passed before release this source code.
  97. using System;
  98. using System.Threading;
  99. using System.Data;
  100. using System.Data.Sql;
  101. using System.Data.SqlClient;
  102. using System.Data.SqlTypes;
  103. using System.Text;
  104. using System.Configuration;
  105. using System.Collections;
  106. using System.Collections.Generic;
  107. using System.Security.Cryptography;
  108. using System.Web;
  109. using System.Web.Security;
  110. using System.Web.UI;
  111. using System.Web.UI.WebControls;
  112. using System.Web.UI.WebControls.WebParts;
  113. using System.Web.UI.HtmlControls;
  114. using System.IO;
  115. using System.Drawing;
  116. using System.Drawing.Imaging;
  117. using System.Drawing.Drawing2D;
  118. using System.Web.Mail;
  119. using System.Reflection;
  120. using System.Runtime.InteropServices;
  121. #if ASPNET
  122. using Resources.BuildUtil.Properties;
  123. #else
  124. using BuildUtil.Properties;
  125. #endif
  126. namespace CoreUtil
  127. {
  128. public static class Con
  129. {
  130. static ConsoleService cs = null;
  131. public static ConsoleService ConsoleService
  132. {
  133. get { return Con.cs; }
  134. }
  135. public static void SetConsoleService(ConsoleService svc)
  136. {
  137. cs = svc;
  138. }
  139. public static void UnsetConsoleService()
  140. {
  141. cs = null;
  142. }
  143. public static string ReadLine()
  144. {
  145. return ReadLine("");
  146. }
  147. public static string ReadLine(string prompt)
  148. {
  149. return ReadLine(prompt, false);
  150. }
  151. public static string ReadLine(string prompt, bool noFile)
  152. {
  153. if (cs != null)
  154. {
  155. return cs.ReadLine(prompt, noFile);
  156. }
  157. else
  158. {
  159. Console.Write(prompt);
  160. return Console.ReadLine();
  161. }
  162. }
  163. public static void WriteLine()
  164. {
  165. WriteLine("");
  166. }
  167. public static void WriteLine(object arg)
  168. {
  169. if (cs != null)
  170. {
  171. cs.WriteLine(arg);
  172. }
  173. else
  174. {
  175. Console.WriteLine(arg);
  176. }
  177. }
  178. public static void WriteLine(string str)
  179. {
  180. if (cs != null)
  181. {
  182. cs.WriteLine(str);
  183. }
  184. else
  185. {
  186. Console.WriteLine(str);
  187. }
  188. }
  189. public static void WriteLine(string str, object arg)
  190. {
  191. if (cs != null)
  192. {
  193. cs.WriteLine(str, arg);
  194. }
  195. else
  196. {
  197. Console.WriteLine(str, arg);
  198. }
  199. }
  200. public static void WriteLine(string str, params object[] args)
  201. {
  202. if (cs != null)
  203. {
  204. cs.WriteLine(str, args);
  205. }
  206. else
  207. {
  208. Console.WriteLine(str, args);
  209. }
  210. }
  211. }
  212. public class ConsoleUserCancelException : Exception
  213. {
  214. public ConsoleUserCancelException(string msg)
  215. : base(msg)
  216. {
  217. }
  218. }
  219. public class ConsoleEvalMinMaxParam
  220. {
  221. public readonly string ErrorMessageString;
  222. public readonly int MinValue, MaxValue;
  223. public ConsoleEvalMinMaxParam(string errorMessageString, int minValue, int maxValue)
  224. {
  225. this.ErrorMessageString = errorMessageString;
  226. this.MinValue = minValue;
  227. this.MaxValue = maxValue;
  228. }
  229. }
  230. public enum ConsoleType
  231. {
  232. Local,
  233. Csv,
  234. }
  235. public class ConsoleParam
  236. {
  237. public readonly string Name;
  238. public readonly ConsolePromptProcDelegate PromptProc;
  239. public readonly object PromptProcParam;
  240. public readonly ConsoleEvalProcDelegate EvalProc;
  241. public readonly object EvalProcParam;
  242. internal string Tmp = null;
  243. public ConsoleParam(string name)
  244. : this(name, null, null)
  245. {
  246. }
  247. public ConsoleParam(string name,
  248. ConsolePromptProcDelegate promptProc,
  249. object promptProcParam)
  250. : this(name, promptProc, promptProcParam, null, null)
  251. {
  252. }
  253. public ConsoleParam(string name,
  254. ConsolePromptProcDelegate promptProc,
  255. object promptProcParam,
  256. ConsoleEvalProcDelegate evalProc,
  257. object evalProcParam)
  258. {
  259. this.Name = name;
  260. this.PromptProc = promptProc;
  261. this.PromptProcParam = promptProcParam;
  262. this.EvalProc = evalProc;
  263. this.EvalProcParam = evalProcParam;
  264. }
  265. }
  266. public delegate string ConsolePromptProcDelegate(ConsoleService c, object param);
  267. public delegate bool ConsoleEvalProcDelegate(ConsoleService c, string str, object param);
  268. delegate void ConsoleFreeDelegate();
  269. delegate string ConsoleReadLineDelegate(string prompt, bool nofile);
  270. delegate string ConsoleReadPasswordDelegate(string prompt);
  271. delegate bool ConsoleWriteDelegate(string str);
  272. delegate int ConsoleGetWidthDelegate();
  273. public class ConsoleParamValueList
  274. {
  275. List<ConsoleParamValue> o;
  276. public ConsoleParamValueList()
  277. {
  278. o = new List<ConsoleParamValue>();
  279. }
  280. public IEnumerable<ConsoleParamValue> Values
  281. {
  282. get
  283. {
  284. int i;
  285. for (i = 0; i < o.Count; i++)
  286. {
  287. yield return o[i];
  288. }
  289. }
  290. }
  291. public void Add(ConsoleParamValue v)
  292. {
  293. if (o.Contains(v) == false)
  294. {
  295. o.Add(v);
  296. }
  297. }
  298. public ConsoleParamValue this[string name]
  299. {
  300. get
  301. {
  302. ConsoleParamValue v = new ConsoleParamValue(name, "", 0);
  303. int i = o.IndexOf(v);
  304. if (i == -1)
  305. {
  306. return new ConsoleParamValue(name, "", 0);
  307. }
  308. return o[i];
  309. }
  310. }
  311. public ConsoleParamValue DefaultParam
  312. {
  313. get
  314. {
  315. foreach (ConsoleParamValue c in o)
  316. {
  317. if (c.IsDefaultParam)
  318. {
  319. return c;
  320. }
  321. }
  322. return new ConsoleParamValue("", "", 0, true);
  323. }
  324. }
  325. public string GetStr(string name)
  326. {
  327. ConsoleParamValue v = this[name];
  328. if (v == null)
  329. {
  330. return null;
  331. }
  332. return v.StrValue;
  333. }
  334. public int GetInt(string name)
  335. {
  336. ConsoleParamValue v = this[name];
  337. if (v == null)
  338. {
  339. return 0;
  340. }
  341. return v.IntValue;
  342. }
  343. public bool GetYes(string name)
  344. {
  345. return Str.StrToBool(name);
  346. }
  347. }
  348. public class ConsoleParamValue : IComparable<ConsoleParamValue>, IEquatable<ConsoleParamValue>
  349. {
  350. public readonly string Name;
  351. public readonly string StrValue;
  352. public readonly int IntValue;
  353. public readonly bool BoolValue;
  354. public readonly bool IsEmpty;
  355. public readonly bool IsDefaultParam;
  356. public ConsoleParamValue(string name, string strValue, int intValue)
  357. : this(name, strValue, intValue, false)
  358. {
  359. }
  360. public ConsoleParamValue(string name, string strValue, int intValue, bool isDefaultParam)
  361. {
  362. this.Name = name;
  363. this.IntValue = intValue;
  364. this.StrValue = strValue;
  365. this.BoolValue = Str.StrToBool(strValue);
  366. this.IsDefaultParam = isDefaultParam;
  367. this.IsEmpty = Str.IsEmptyStr(strValue);
  368. }
  369. public int CompareTo(ConsoleParamValue other)
  370. {
  371. return Str.StrCmpiRetInt(this.Name, other.Name);
  372. }
  373. public bool Equals(ConsoleParamValue other)
  374. {
  375. return Str.StrCmpi(this.Name, other.Name);
  376. }
  377. }
  378. public class ConsoleCommandParam : Attribute
  379. {
  380. }
  381. public class ConsoleCommandMethod : Attribute
  382. {
  383. public readonly string Description;
  384. public readonly string ArgsHelp;
  385. public readonly string BodyHelp;
  386. public readonly SortedList<string, string> ParamHelp;
  387. internal BindingFlags bindingFlag;
  388. internal MemberInfo memberInfo;
  389. internal MethodInfo methodInfo;
  390. internal string name;
  391. public ConsoleCommandMethod(string description, string argsHelp, string bodyHelp, params string[] paramHelp)
  392. {
  393. this.Description = description;
  394. this.ArgsHelp = argsHelp;
  395. this.BodyHelp = bodyHelp;
  396. this.ParamHelp = new SortedList<string, string>(new StrComparer(false));
  397. foreach (string s in paramHelp)
  398. {
  399. int i = s.IndexOf(":");
  400. if (i == -1)
  401. {
  402. throw new ArgumentException(s);
  403. }
  404. this.ParamHelp.Add(s.Substring(0, i), s.Substring(i + 1));
  405. }
  406. }
  407. }
  408. public static class ConsoleErrorCode
  409. {
  410. public const int ERR_BAD_COMMAND_OR_PARAM = -100001;
  411. public const int ERR_INNER_EXCEPTION = -100002;
  412. public const int ERR_USER_CANCELED = -100003;
  413. public static string ErrorCodeToString(int code)
  414. {
  415. bool b;
  416. return ErrorCodeToString(code, out b);
  417. }
  418. public static string ErrorCodeToString(int code, out bool unknownError)
  419. {
  420. unknownError = false;
  421. switch (code)
  422. {
  423. case ERR_BAD_COMMAND_OR_PARAM:
  424. return "Bad command or parameters.";
  425. case ERR_USER_CANCELED:
  426. return "User canceled.";
  427. case ERR_INNER_EXCEPTION:
  428. default:
  429. unknownError = true;
  430. return string.Format("Unknown Error {0}", code);
  431. }
  432. }
  433. }
  434. public class ConsoleService
  435. {
  436. IO inFile;
  437. Buf inBuf;
  438. IO outFile;
  439. int win32_OldConsoleWidth;
  440. public const int MaxPromptStrSize = 65536;
  441. public const int Win32DefaultConsoleWidth = 100;
  442. ConsoleType consoleType;
  443. public ConsoleType ConsoleType
  444. {
  445. get { return consoleType; }
  446. }
  447. int retCode;
  448. public int RetCode
  449. {
  450. get { return retCode; }
  451. }
  452. string retErrorMessage;
  453. public string RetErrorMessage
  454. {
  455. get
  456. {
  457. bool b;
  458. string s = ConsoleErrorCode.ErrorCodeToString(this.RetCode, out b);
  459. if (b)
  460. {
  461. s = this.retErrorMessage;
  462. }
  463. Str.NormalizeString(ref s);
  464. return s;
  465. }
  466. }
  467. ConsoleFreeDelegate free;
  468. ConsoleReadLineDelegate readLine;
  469. ConsoleReadPasswordDelegate readPassword;
  470. ConsoleWriteDelegate write;
  471. ConsoleGetWidthDelegate getWidth;
  472. SortedList<string, ConsoleCommandMethod> currentCmdList = null;
  473. private ConsoleService()
  474. {
  475. }
  476. public static int EntryPoint(string cmdLine, string programName, Type commandClass)
  477. {
  478. string s;
  479. return EntryPoint(cmdLine, programName, commandClass, out s);
  480. }
  481. public static int EntryPoint(string cmdLine, string programName, Type commandClass, out string lastErrorMessage)
  482. {
  483. int ret = 0;
  484. string infile, outfile;
  485. string csvmode;
  486. ConsoleService c;
  487. lastErrorMessage = "";
  488. infile = ParseCommand(cmdLine, "in");
  489. outfile = ParseCommand(cmdLine, "out");
  490. if (Str.IsEmptyStr(infile))
  491. {
  492. infile = null;
  493. }
  494. if (Str.IsEmptyStr(outfile))
  495. {
  496. outfile = null;
  497. }
  498. c = ConsoleService.NewLocalConsoleService(infile, outfile);
  499. csvmode = ParseCommand(cmdLine, "csv");
  500. if (csvmode != null)
  501. {
  502. c.consoleType = ConsoleType.Csv;
  503. }
  504. if (c.DispatchCommand(cmdLine, ">", commandClass) == false)
  505. {
  506. ret = ConsoleErrorCode.ERR_BAD_COMMAND_OR_PARAM;
  507. }
  508. else
  509. {
  510. ret = c.retCode;
  511. }
  512. lastErrorMessage = c.RetErrorMessage;
  513. return ret;
  514. }
  515. public bool WriteLine(object value)
  516. {
  517. return WriteLine(value.ToString());
  518. }
  519. public bool WriteLine(string str)
  520. {
  521. return localWrite(str);
  522. }
  523. public bool WriteLine(string format, object arg0)
  524. {
  525. return WriteLine(string.Format(format, arg0));
  526. }
  527. public bool WriteLine(string format, params object[] arg)
  528. {
  529. return WriteLine(string.Format(format, arg));
  530. }
  531. public string ReadLine(string prompt)
  532. {
  533. return ReadLine(prompt, false);
  534. }
  535. public string ReadLine(string prompt, bool noFile)
  536. {
  537. return localReadLine(prompt, noFile);
  538. }
  539. public string ReadPassword(string prompt)
  540. {
  541. return localReadPassword(prompt);
  542. }
  543. public static ConsolePromptProcDelegate Prompt
  544. {
  545. get { return new ConsolePromptProcDelegate(prompt); }
  546. }
  547. static string prompt(ConsoleService c, object param)
  548. {
  549. string p = (param == null) ? Resources.CMD_PROMPT : (string)param;
  550. return c.readLine(p, true);
  551. }
  552. public static ConsoleEvalProcDelegate EvalIsFile
  553. {
  554. get { return new ConsoleEvalProcDelegate(evalIsFile); }
  555. }
  556. static bool evalIsFile(ConsoleService c, string str, object param)
  557. {
  558. string tmp;
  559. if (c == null || str == null)
  560. {
  561. return false;
  562. }
  563. tmp = str;
  564. if (Str.IsEmptyStr(tmp))
  565. {
  566. c.write(Resources.CMD_FILE_NAME_EMPTY);
  567. return false;
  568. }
  569. if (IO.IsFileExists(tmp) == false)
  570. {
  571. c.write(Str.FormatC(Resources.CMD_FILE_NOT_FOUND, tmp));
  572. return false;
  573. }
  574. return true;
  575. }
  576. public static ConsoleEvalProcDelegate EvalInt1
  577. {
  578. get { return new ConsoleEvalProcDelegate(evalInt1); }
  579. }
  580. static bool evalInt1(ConsoleService c, string str, object param)
  581. {
  582. string p = (param == null) ? Resources.CMD_EVAL_INT : (string)param;
  583. if (Str.StrToInt(str) == 0)
  584. {
  585. c.write(p);
  586. return false;
  587. }
  588. return true;
  589. }
  590. public static ConsoleEvalProcDelegate EvalNotEmpty
  591. {
  592. get { return new ConsoleEvalProcDelegate(evalNotEmpty); }
  593. }
  594. static bool evalNotEmpty(ConsoleService c, string str, object param)
  595. {
  596. string p = (param == null) ? Resources.CMD_EVAL_NOT_EMPTY : (string)param;
  597. if (Str.IsEmptyStr(str) == false)
  598. {
  599. return true;
  600. }
  601. c.write(p);
  602. return false;
  603. }
  604. public static ConsoleEvalProcDelegate EvalMinMax
  605. {
  606. get { return new ConsoleEvalProcDelegate(evalMinMax); }
  607. }
  608. static bool evalMinMax(ConsoleService c, string str, object param)
  609. {
  610. string tag;
  611. int v;
  612. if (param == null)
  613. {
  614. return false;
  615. }
  616. ConsoleEvalMinMaxParam e = (ConsoleEvalMinMaxParam)param;
  617. if (Str.IsEmptyStr(e.ErrorMessageString))
  618. {
  619. tag = Resources.CMD_EVAL_MIN_MAX;
  620. }
  621. else
  622. {
  623. tag = e.ErrorMessageString;
  624. }
  625. v = Str.StrToInt(str);
  626. if (v >= e.MinValue && v <= e.MaxValue)
  627. {
  628. return true;
  629. }
  630. else
  631. {
  632. c.write(Str.FormatC(tag, e.MinValue, e.MaxValue));
  633. return false;
  634. }
  635. }
  636. public void PrintCmdHelp(string cmdName, List<string> paramList)
  637. {
  638. string tmp;
  639. string buf;
  640. string description, args, help;
  641. List<string> t;
  642. int width;
  643. int i;
  644. string space;
  645. if (cmdName == null || paramList == null)
  646. {
  647. return;
  648. }
  649. width = GetConsoleWidth() - 2;
  650. description = this.currentCmdList[cmdName].Description;
  651. args = this.currentCmdList[cmdName].ArgsHelp;
  652. help = this.currentCmdList[cmdName].BodyHelp;
  653. space = Str.MakeCharArray(' ', 2);
  654. tmp = Str.FormatC(Resources.CMD_HELP_TITLE, cmdName);
  655. this.write(tmp);
  656. this.write("");
  657. this.write(Resources.CMD_HELP_DESCRIPTION);
  658. t = Str.StrArrayToList(SeparateStringByWidth(description, width - 2));
  659. for (i = 0; i < t.Count; i++)
  660. {
  661. buf = Str.FormatC("%S%s", space, t[i]);
  662. this.write(buf);
  663. }
  664. this.write("");
  665. this.write(Resources.CMD_HELP_HELP);
  666. t = Str.StrArrayToList(SeparateStringByWidth(help, width - 2));
  667. for (i = 0; i < t.Count; i++)
  668. {
  669. buf = Str.FormatC("%S%s", space, t[i]);
  670. this.write(buf);
  671. }
  672. this.write("");
  673. this.write(Resources.CMD_HELP_USAGE);
  674. t = Str.StrArrayToList(SeparateStringByWidth(args, width - 2));
  675. for (i = 0; i < t.Count; i++)
  676. {
  677. buf = Str.FormatC("%S%s", space, t[i]);
  678. this.write(buf);
  679. }
  680. if (paramList.Count >= 1)
  681. {
  682. this.write("");
  683. this.write(Resources.CMD_HELP_ARGS);
  684. PrintCandidateHelp(cmdName, paramList.ToArray(), 2, this.currentCmdList);
  685. }
  686. }
  687. public void PrintCandidateHelp(string cmdName, string[] candidateList, int leftSpace, SortedList<string, ConsoleCommandMethod> ccList)
  688. {
  689. int console_width;
  690. int max_keyword_width;
  691. List<string> o;
  692. int i;
  693. string tmpbuf;
  694. string left_space_array;
  695. string max_space_array;
  696. if (candidateList == null)
  697. {
  698. return;
  699. }
  700. console_width = GetConsoleWidth() - 1;
  701. left_space_array = Str.MakeCharArray(' ', leftSpace);
  702. o = new List<string>();
  703. max_keyword_width = 0;
  704. for (i = 0; i < candidateList.Length; i++)
  705. {
  706. int keyword_width;
  707. o.Add(candidateList[i]);
  708. keyword_width = Str.GetStrWidth(candidateList[i]);
  709. if (cmdName != null)
  710. {
  711. if (candidateList[i].StartsWith("[", StringComparison.InvariantCultureIgnoreCase) == false)
  712. {
  713. keyword_width += 1;
  714. }
  715. else
  716. {
  717. keyword_width -= 2;
  718. }
  719. }
  720. max_keyword_width = Math.Max(max_keyword_width, keyword_width);
  721. }
  722. max_space_array = Str.MakeCharArray(' ', max_keyword_width);
  723. for (i = 0; i < o.Count; i++)
  724. {
  725. string tmp;
  726. string name = o[i];
  727. List<string> t;
  728. string help;
  729. int j;
  730. int keyword_start_width = leftSpace;
  731. int descript_start_width = leftSpace + max_keyword_width + 1;
  732. int descript_width;
  733. string space;
  734. if (console_width >= (descript_start_width + 5))
  735. {
  736. descript_width = console_width - descript_start_width - 3;
  737. }
  738. else
  739. {
  740. descript_width = 2;
  741. }
  742. if (cmdName != null && name.StartsWith("[", StringComparison.InvariantCultureIgnoreCase) == false)
  743. {
  744. tmp = Str.FormatC("/%s", name);
  745. }
  746. else
  747. {
  748. if (cmdName == null)
  749. {
  750. tmp = name;
  751. }
  752. else
  753. {
  754. if (name.Length >= 1)
  755. {
  756. tmp = name.Substring(1);
  757. }
  758. else
  759. {
  760. tmp = "";
  761. }
  762. if (tmp.Length >= 1)
  763. {
  764. tmp = tmp.Substring(0, tmp.Length - 1);
  765. }
  766. }
  767. }
  768. if (cmdName == null)
  769. {
  770. help = ccList[name].Description;
  771. }
  772. else
  773. {
  774. if (ccList[cmdName].ParamHelp.ContainsKey(name))
  775. {
  776. help = ccList[cmdName].ParamHelp[name];
  777. }
  778. else
  779. {
  780. help = Resources.CMD_UNKNOWN_PARAM;
  781. }
  782. }
  783. space = Str.MakeCharArray(' ', max_keyword_width - Str.GetStrWidth(name) -
  784. (cmdName == null ? 0 : (name.StartsWith("[", StringComparison.InvariantCultureIgnoreCase) == false ? 1 : -2)));
  785. t = Str.StrArrayToList(SeparateStringByWidth(help, descript_width));
  786. for (j = 0; j < t.Count; j++)
  787. {
  788. if (j == 0)
  789. {
  790. tmpbuf = Str.FormatC("%S%S%S - %s",
  791. left_space_array, tmp, space, t[j]);
  792. }
  793. else
  794. {
  795. tmpbuf = Str.FormatC("%S%S %s",
  796. left_space_array, max_space_array, t[j]);
  797. }
  798. this.write(tmpbuf);
  799. }
  800. }
  801. }
  802. public static string[] SeparateStringByWidth(string str, int width)
  803. {
  804. if (str == null)
  805. {
  806. return new string[0];
  807. }
  808. if (width <= 0)
  809. {
  810. width = 1;
  811. }
  812. StringBuilder tmp = new StringBuilder();
  813. int len, i;
  814. List<string> o = new List<string>();
  815. str += (char)0;
  816. len = str.Length;
  817. for (i = 0; i < len; i++)
  818. {
  819. char c = str[i];
  820. switch (c)
  821. {
  822. case (char)0:
  823. case '\r':
  824. case '\n':
  825. if (c == '\r')
  826. {
  827. if (str[i + 1] == '\n')
  828. {
  829. i++;
  830. }
  831. }
  832. o.Add(tmp.ToString());
  833. tmp = new StringBuilder();
  834. break;
  835. default:
  836. tmp.Append(c);
  837. if (Str.GetStrWidth(tmp.ToString()) >= width)
  838. {
  839. o.Add(tmp.ToString());
  840. tmp = new StringBuilder();
  841. }
  842. break;
  843. }
  844. }
  845. if (o.Count == 0)
  846. {
  847. o.Add("");
  848. }
  849. return o.ToArray();
  850. }
  851. public static bool IsHelpStr(string str)
  852. {
  853. if (str == null)
  854. {
  855. return false;
  856. }
  857. if (Str.IsStrInList(str, true,
  858. "help", "?", "man", "/man", "-man", "--man",
  859. "/help", "/?", "-help", "-?",
  860. "/h", "--help", "--?"))
  861. {
  862. return true;
  863. }
  864. return false;
  865. }
  866. public bool DispatchCommand(string execCommandOrNull, string prompt, Type commandClass)
  867. {
  868. return DispatchCommand(execCommandOrNull, prompt, commandClass, null);
  869. }
  870. public bool DispatchCommand(string execCommandOrNull, string prompt, Type commandClass, object invokerInstance)
  871. {
  872. SortedList<string, ConsoleCommandMethod> cmdList = GetCommandList(commandClass);
  873. currentCmdList = cmdList;
  874. try
  875. {
  876. string str, tmp, cmd_name;
  877. bool b_exit = false;
  878. string cmd_param;
  879. int ret = 0;
  880. List<string> t, candidate;
  881. int i;
  882. if (Str.IsEmptyStr(execCommandOrNull))
  883. {
  884. RETRY:
  885. tmp = prompt;
  886. str = this.readLine(tmp, false);
  887. if (str != null && Str.IsEmptyStr(str))
  888. {
  889. goto RETRY;
  890. }
  891. }
  892. else
  893. {
  894. if (prompt != null)
  895. {
  896. if (this.consoleType != ConsoleType.Csv)
  897. {
  898. }
  899. }
  900. str = execCommandOrNull;
  901. }
  902. if (str == null)
  903. {
  904. return false;
  905. }
  906. str = Str.TrimCrlf(str).Trim();
  907. if (Str.IsEmptyStr(str))
  908. {
  909. return true;
  910. }
  911. if (SeparateCommandAndParam(str, out cmd_name, out cmd_param) == false)
  912. {
  913. return true;
  914. }
  915. if (cmd_name.Length >= 2 && cmd_name[0] == '?' && cmd_name[1] != '?')
  916. {
  917. cmd_name = cmd_name.Substring(1);
  918. cmd_param = "/?";
  919. }
  920. if (cmd_name.Length >= 2 && cmd_name.EndsWith("?") && cmd_name[cmd_name.Length - 2] != '?')
  921. {
  922. cmd_name = cmd_name.Substring(0, cmd_name.Length - 1);
  923. cmd_param = "/?";
  924. }
  925. t = new List<string>();
  926. for (i = 0; i < cmdList.Count; i++)
  927. {
  928. t.Add(cmdList.Keys[i]);
  929. }
  930. if (IsHelpStr(cmd_name))
  931. {
  932. if (Str.IsEmptyStr(cmd_param))
  933. {
  934. this.write(Str.FormatC(Resources.CMD_HELP_1, t.Count));
  935. string[] candidateList = t.ToArray();
  936. PrintCandidateHelp(null, candidateList, 1, cmdList);
  937. this.write("");
  938. this.write(Resources.CMD_HELP_2);
  939. }
  940. else
  941. {
  942. string tmp2, tmp3;
  943. if (SeparateCommandAndParam(cmd_param, out tmp2, out tmp3))
  944. {
  945. bool b = true;
  946. if (IsHelpStr(tmp2))
  947. {
  948. b = false;
  949. }
  950. if (b)
  951. {
  952. DispatchCommand(Str.FormatC("%S /help", tmp2), null, commandClass, invokerInstance);
  953. }
  954. }
  955. }
  956. }
  957. else if (Str.StrCmpi(cmd_name, "exit") ||
  958. Str.StrCmpi(cmd_name, "quit"))
  959. {
  960. b_exit = true;
  961. }
  962. else
  963. {
  964. candidate = Str.StrArrayToList(GetRealnameCandidate(cmd_name, t.ToArray()));
  965. if (candidate == null || candidate.Count == 0)
  966. {
  967. this.write(Str.FormatC(Resources.CON_UNKNOWN_CMD, cmd_name));
  968. this.retCode = ConsoleErrorCode.ERR_BAD_COMMAND_OR_PARAM;
  969. }
  970. else if (candidate.Count >= 2)
  971. {
  972. this.write(Str.FormatC(Resources.CON_AMBIGIOUS_CMD, cmd_name));
  973. this.write(Resources.CON_AMBIGIOUS_CMD_1);
  974. string[] candidateArray = candidate.ToArray();
  975. PrintCandidateHelp(null, candidateArray, 1, cmdList);
  976. this.write(Resources.CON_AMBIGIOUS_CMD_2);
  977. this.retCode = ConsoleErrorCode.ERR_BAD_COMMAND_OR_PARAM;
  978. }
  979. else
  980. {
  981. string real_cmd_name;
  982. int j;
  983. real_cmd_name = candidate[0];
  984. for (j = 0; j < cmdList.Count; j++)
  985. {
  986. if (Str.Equals(cmdList.Values[j].name, real_cmd_name))
  987. {
  988. if (this.consoleType != ConsoleType.Csv)
  989. {
  990. this.write(Str.FormatC(Resources.CMD_EXEC_MSG_NAME,
  991. cmdList.Values[j].name,
  992. cmdList.Values[j].Description));
  993. }
  994. object srcObject = null;
  995. if (cmdList.Values[j].methodInfo.IsStatic == false)
  996. {
  997. srcObject = invokerInstance;
  998. }
  999. object[] paramList =
  1000. {
  1001. this,
  1002. real_cmd_name,
  1003. cmd_param,
  1004. };
  1005. try
  1006. {
  1007. ret = (int)cmdList.Values[j].methodInfo.Invoke(srcObject, paramList);
  1008. }
  1009. catch (TargetInvocationException ex)
  1010. {
  1011. Exception ex2 = ex.GetBaseException();
  1012. if (ex2 is ConsoleUserCancelException)
  1013. {
  1014. this.write(Resources.CON_USER_CANCELED);
  1015. this.write("");
  1016. this.retCode = ConsoleErrorCode.ERR_USER_CANCELED;
  1017. }
  1018. else
  1019. {
  1020. this.write(ex2.ToString());
  1021. this.write("");
  1022. this.retCode = ConsoleErrorCode.ERR_INNER_EXCEPTION;
  1023. this.retErrorMessage = ex2.Message;
  1024. }
  1025. return true;
  1026. }
  1027. if (ret == -1)
  1028. {
  1029. b_exit = true;
  1030. }
  1031. else
  1032. {
  1033. this.retCode = ret;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. if (b_exit)
  1040. {
  1041. return false;
  1042. }
  1043. return true;
  1044. }
  1045. finally
  1046. {
  1047. currentCmdList = null;
  1048. }
  1049. }
  1050. public static SortedList<string, ConsoleCommandMethod> GetCommandList(Type commandClass)
  1051. {
  1052. SortedList<string, ConsoleCommandMethod> cmdList = new SortedList<string, ConsoleCommandMethod>(new StrComparer(false));
  1053. BindingFlags[] searchFlags =
  1054. {
  1055. BindingFlags.Static | BindingFlags.NonPublic,
  1056. BindingFlags.Static | BindingFlags.Public,
  1057. BindingFlags.Instance | BindingFlags.NonPublic,
  1058. BindingFlags.Instance | BindingFlags.Public,
  1059. };
  1060. foreach (BindingFlags bFlag in searchFlags)
  1061. {
  1062. MemberInfo[] members = commandClass.GetMembers(bFlag);
  1063. foreach (MemberInfo info in members)
  1064. {
  1065. if ((info.MemberType & MemberTypes.Method) != 0)
  1066. {
  1067. MethodInfo mInfo = commandClass.GetMethod(info.Name, bFlag);
  1068. object[] customAtts = mInfo.GetCustomAttributes(true);
  1069. foreach (object att in customAtts)
  1070. {
  1071. if (att is ConsoleCommandMethod)
  1072. {
  1073. ConsoleCommandMethod cc = (ConsoleCommandMethod)att;
  1074. cc.bindingFlag = bFlag;
  1075. cc.memberInfo = info;
  1076. cc.methodInfo = mInfo;
  1077. cc.name = info.Name;
  1078. cmdList.Add(info.Name, cc);
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. }
  1085. return cmdList;
  1086. }
  1087. public int GetConsoleWidth()
  1088. {
  1089. int size = this.getWidth();
  1090. if (size == 0)
  1091. {
  1092. size = 80;
  1093. }
  1094. if (size < 32)
  1095. {
  1096. size = 32;
  1097. }
  1098. if (size > 65535)
  1099. {
  1100. size = 65535;
  1101. }
  1102. return size;
  1103. }
  1104. public static bool SeparateCommandAndParam(string src, out string cmd, out string param)
  1105. {
  1106. int i, len;
  1107. StringBuilder tmp;
  1108. string src_tmp;
  1109. cmd = param = null;
  1110. if (src == null)
  1111. {
  1112. return false;
  1113. }
  1114. src_tmp = Str.TrimCrlf(src).Trim();
  1115. len = src_tmp.Length;
  1116. tmp = new StringBuilder();
  1117. for (i = 0; i < (len + 1); i++)
  1118. {
  1119. char c;
  1120. if (i != len)
  1121. {
  1122. c = src_tmp[i];
  1123. }
  1124. else
  1125. {
  1126. c = (char)0;
  1127. }
  1128. switch (c)
  1129. {
  1130. case (char)0:
  1131. case ' ':
  1132. case '\t':
  1133. if (Str.IsEmptyStr(tmp.ToString()))
  1134. {
  1135. return false;
  1136. }
  1137. cmd = tmp.ToString().Trim();
  1138. goto ESCAPE;
  1139. default:
  1140. tmp.Append(c);
  1141. break;
  1142. }
  1143. }
  1144. ESCAPE:
  1145. param = src_tmp.Substring(tmp.Length).Trim();
  1146. return true;
  1147. }
  1148. public static string[] GetRealnameCandidate(string inputName, string[] realNameList)
  1149. {
  1150. List<string> o = new List<string>();
  1151. if (inputName == null || realNameList == null)
  1152. {
  1153. return new string[0];
  1154. }
  1155. int i;
  1156. bool ok = false;
  1157. for (i = 0; i < realNameList.Length; i++)
  1158. {
  1159. string name = realNameList[i];
  1160. if (Str.StrCmpi(name, inputName))
  1161. {
  1162. o.Add(name);
  1163. ok = true;
  1164. break;
  1165. }
  1166. }
  1167. if (ok == false)
  1168. {
  1169. for (i = 0; i < realNameList.Length; i++)
  1170. {
  1171. string name = realNameList[i];
  1172. if (IsOmissionName(inputName, name) ||
  1173. IsNameInRealName(inputName, name))
  1174. {
  1175. o.Add(name);
  1176. ok = true;
  1177. }
  1178. }
  1179. }
  1180. if (ok)
  1181. {
  1182. return o.ToArray();
  1183. }
  1184. else
  1185. {
  1186. return new string[0];
  1187. }
  1188. }
  1189. public static bool IsOmissionName(string inputName, string realName)
  1190. {
  1191. string oname;
  1192. if (inputName == null || realName == null)
  1193. {
  1194. return false;
  1195. }
  1196. if (Str.IsAllUpperStr(realName))
  1197. {
  1198. return false;
  1199. }
  1200. oname = GetOmissionName(realName);
  1201. if (Str.IsEmptyStr(oname))
  1202. {
  1203. return false;
  1204. }
  1205. if (oname.StartsWith(inputName, StringComparison.InvariantCultureIgnoreCase))
  1206. {
  1207. return true;
  1208. }
  1209. if (inputName.StartsWith(oname, StringComparison.InvariantCultureIgnoreCase))
  1210. {
  1211. if (realName.EndsWith(inputName.Substring(oname.Length), StringComparison.InvariantCultureIgnoreCase))
  1212. {
  1213. return true;
  1214. }
  1215. }
  1216. return false;
  1217. }
  1218. public static string GetOmissionName(string src)
  1219. {
  1220. int i, len;
  1221. if (src == null)
  1222. {
  1223. return null;
  1224. }
  1225. string dst = "";
  1226. len = src.Length;
  1227. for (i = 0; i < len; i++)
  1228. {
  1229. char c = src[i];
  1230. if ((c >= '0' && c <= '9') ||
  1231. (c >= 'A' && c <= 'Z'))
  1232. {
  1233. dst += c;
  1234. }
  1235. }
  1236. return dst;
  1237. }
  1238. public static bool IsNameInRealName(string inputName, string realName)
  1239. {
  1240. if (inputName == null || realName == null)
  1241. {
  1242. return false;
  1243. }
  1244. if (realName.StartsWith(inputName, StringComparison.InvariantCultureIgnoreCase))
  1245. {
  1246. return true;
  1247. }
  1248. return false;
  1249. }
  1250. public ConsoleParamValueList ParseCommandList(string cmdName, string command, ConsoleParam[] param)
  1251. {
  1252. ConsoleParamValueList ret = parseCommandLineMain(cmdName, command, param);
  1253. if (ret == null)
  1254. {
  1255. throw new ConsoleUserCancelException("");
  1256. }
  1257. return ret;
  1258. }
  1259. private ConsoleParamValueList parseCommandLineMain(string cmdName, string command, ConsoleParam[] param)
  1260. {
  1261. int i;
  1262. ConsoleParamValueList o;
  1263. List<string> param_list;
  1264. List<string> real_name_list;
  1265. bool help_mode = false;
  1266. string tmp;
  1267. bool ok = true;
  1268. if (command == null || cmdName == null)
  1269. {
  1270. return null;
  1271. }
  1272. for (i = 0; i < param.Length; i++)
  1273. {
  1274. if (Str.IsEmptyStr(param[i].Name) == false)
  1275. {
  1276. if (param[i].Name.StartsWith("["))
  1277. {
  1278. param[i].Tmp = "";
  1279. }
  1280. else
  1281. {
  1282. param[i].Tmp = null;
  1283. }
  1284. }
  1285. else
  1286. {
  1287. param[i].Tmp = "";
  1288. }
  1289. }
  1290. param_list = Str.StrArrayToList(GetCommandNameList(command));
  1291. real_name_list = new List<string>();
  1292. for (i = 0; i < param.Length; i++)
  1293. {
  1294. real_name_list.Add(param[i].Name);
  1295. }
  1296. for (i = 0; i < param_list.Count; i++)
  1297. {
  1298. string s = param_list[i];
  1299. if (Str.StrCmpi(s, "help") ||
  1300. Str.StrCmpi(s, "?"))
  1301. {
  1302. help_mode = true;
  1303. break;
  1304. }
  1305. }
  1306. tmp = ParseCommand(command, "");
  1307. if (tmp != null)
  1308. {
  1309. if (Str.StrCmpi(tmp, "?"))
  1310. {
  1311. help_mode = true;
  1312. }
  1313. }
  1314. if (help_mode)
  1315. {
  1316. PrintCmdHelp(cmdName, real_name_list);
  1317. return null;
  1318. }
  1319. for (i = 0; i < param_list.Count; i++)
  1320. {
  1321. string[] candidate = GetRealnameCandidate(param_list[i], real_name_list.ToArray());
  1322. if (candidate != null && candidate.Length >= 1)
  1323. {
  1324. if (candidate.Length >= 2)
  1325. {
  1326. this.write(Str.FormatC(Resources.CON_AMBIGIOUS_PARAM,
  1327. param_list[i]));
  1328. this.write(Str.FormatC(Resources.CON_AMBIGIOUS_PARAM_1,
  1329. cmdName));
  1330. PrintCandidateHelp(cmdName, candidate, 1, this.currentCmdList);
  1331. this.write(Resources.CON_AMBIGIOUS_PARAM_2);
  1332. ok = false;
  1333. }
  1334. else
  1335. {
  1336. int j;
  1337. string real_name = candidate[0];
  1338. for (j = 0; j < param.Length; j++)
  1339. {
  1340. if (Str.StrCmpi(param[j].Name, real_name))
  1341. {
  1342. param[j].Tmp = param_list[i];
  1343. }
  1344. }
  1345. }
  1346. }
  1347. else
  1348. {
  1349. this.write(Str.FormatC(Resources.CON_INVALID_PARAM,
  1350. param_list[i],
  1351. cmdName,
  1352. cmdName));
  1353. ok = false;
  1354. }
  1355. }
  1356. if (ok == false)
  1357. {
  1358. return null;
  1359. }
  1360. o = new ConsoleParamValueList();
  1361. for (i = 0; i < param.Length; i++)
  1362. {
  1363. ConsoleParam p = param[i];
  1364. bool is_default_value = false;
  1365. if (p.Tmp == "")
  1366. {
  1367. is_default_value = true;
  1368. }
  1369. if (p.Tmp != null || p.PromptProc != null)
  1370. {
  1371. string name = p.Name;
  1372. string tmp2, str;
  1373. if (p.Tmp != null)
  1374. {
  1375. tmp2 = p.Tmp;
  1376. }
  1377. else
  1378. {
  1379. tmp2 = p.Name;
  1380. }
  1381. str = ParseCommand(command, tmp2);
  1382. if (str != null)
  1383. {
  1384. string unistr;
  1385. bool ret;
  1386. EVAL_VALUE:
  1387. unistr = str;
  1388. if (p.EvalProc != null)
  1389. {
  1390. ret = p.EvalProc(this, unistr, p.EvalProcParam);
  1391. }
  1392. else
  1393. {
  1394. ret = true;
  1395. }
  1396. if (ret == false)
  1397. {
  1398. string tmp3;
  1399. if (p.PromptProc == null)
  1400. {
  1401. ok = false;
  1402. break;
  1403. }
  1404. else
  1405. {
  1406. str = null;
  1407. tmp3 = p.PromptProc(this, p.PromptProcParam);
  1408. if (tmp3 == null)
  1409. {
  1410. ok = false;
  1411. break;
  1412. }
  1413. else
  1414. {
  1415. this.write("");
  1416. str = tmp3;
  1417. goto EVAL_VALUE;
  1418. }
  1419. }
  1420. }
  1421. else
  1422. {
  1423. o.Add(new ConsoleParamValue(p.Name, str, Str.StrToInt(str), is_default_value));
  1424. }
  1425. }
  1426. else
  1427. {
  1428. if (p.PromptProc != null)
  1429. {
  1430. string tmp4;
  1431. tmp4 = p.PromptProc(this, p.PromptProcParam);
  1432. if (tmp4 == null)
  1433. {
  1434. ok = false;
  1435. break;
  1436. }
  1437. else
  1438. {
  1439. this.write("");
  1440. str = tmp4;
  1441. if (true)
  1442. {
  1443. string unistr;
  1444. bool ret;
  1445. EVAL_VALUE:
  1446. unistr = str;
  1447. if (p.EvalProc != null)
  1448. {
  1449. ret = p.EvalProc(this, unistr, p.EvalProcParam);
  1450. }
  1451. else
  1452. {
  1453. ret = true;
  1454. }
  1455. if (ret == false)
  1456. {
  1457. if (p.PromptProc == null)
  1458. {
  1459. ok = false;
  1460. break;
  1461. }
  1462. else
  1463. {
  1464. str = null;
  1465. tmp4 = p.PromptProc(this, p.PromptProcParam);
  1466. if (tmp4 == null)
  1467. {
  1468. ok = false;
  1469. break;
  1470. }
  1471. else
  1472. {
  1473. this.write("");
  1474. str = tmp4;
  1475. goto EVAL_VALUE;
  1476. }
  1477. }
  1478. }
  1479. else
  1480. {
  1481. o.Add(new ConsoleParamValue(p.Name, str, Str.StrToInt(str), is_default_value));
  1482. }
  1483. }
  1484. }
  1485. }
  1486. }
  1487. }
  1488. }
  1489. if (ok)
  1490. {
  1491. return o;
  1492. }
  1493. else
  1494. {
  1495. return null;
  1496. }
  1497. }
  1498. public static string[] GetCommandNameList(string str)
  1499. {
  1500. if (str == null)
  1501. {
  1502. return new string[0];
  1503. }
  1504. string[] pl;
  1505. ParseCommand(str, "dummy_str", out pl);
  1506. return pl;
  1507. }
  1508. public static string ParseCommand(string str, string name)
  1509. {
  1510. string[] pl;
  1511. return ParseCommand(str, name, out pl);
  1512. }
  1513. public static string ParseCommand(string str, string name, out string[] paramList)
  1514. {
  1515. int i;
  1516. string tmp, ret = null;
  1517. SortedList<string, int> o;
  1518. paramList = null;
  1519. if (str == null)
  1520. {
  1521. return null;
  1522. }
  1523. if (Str.IsEmptyStr(name))
  1524. {
  1525. name = null;
  1526. }
  1527. o = new SortedList<string, int>(new StrComparer(false));
  1528. tmp = str.Trim();
  1529. i = Str.SearchStr(tmp, "/CMD", 0, false);
  1530. if (i >= 1 && tmp[i - 1] == '/')
  1531. {
  1532. i = -1;
  1533. }
  1534. if (i == -1)
  1535. {
  1536. i = Str.SearchStr(tmp, "/CMD\t", 0, false);
  1537. if (i >= 1 && tmp[i - 1] == '/')
  1538. {
  1539. i = -1;
  1540. }
  1541. }
  1542. if (i == -1)
  1543. {
  1544. i = Str.SearchStr(tmp, "/CMD:", 0, false);
  1545. if (i >= 1 && tmp[i - 1] == '/')
  1546. {
  1547. i = -1;
  1548. }
  1549. }
  1550. if (i == -1)
  1551. {
  1552. i = Str.SearchStr(tmp, "/CMD=", 0, false);
  1553. if (i >= 1 && tmp[i - 1] == '/')
  1554. {
  1555. i = -1;
  1556. }
  1557. }
  1558. if (i == -1)
  1559. {
  1560. i = Str.SearchStr(tmp, "-CMD ", 0, false);
  1561. if (i >= 1 && tmp[i - 1] == '-')
  1562. {
  1563. i = -1;
  1564. }
  1565. }
  1566. if (i == -1)
  1567. {
  1568. i = Str.SearchStr(tmp, "-CMD\t", 0, false);
  1569. if (i >= 1 && tmp[i - 1] == '-')
  1570. {
  1571. i = -1;
  1572. }
  1573. }
  1574. if (i == -1)
  1575. {
  1576. i = Str.SearchStr(tmp, "-CMD:", 0, false);
  1577. if (i >= 1 && tmp[i - 1] == '-')
  1578. {
  1579. i = -1;
  1580. }
  1581. }
  1582. if (i == -1)
  1583. {
  1584. i = Str.SearchStr(tmp, "-CMD=", 0, false);
  1585. if (i >= 1 && tmp[i - 1] == '-')
  1586. {
  1587. i = -1;
  1588. }
  1589. }
  1590. if (i != -1)
  1591. {
  1592. string s = "CMD";
  1593. if (o != null)
  1594. {
  1595. if (o.ContainsKey(s) == false)
  1596. {
  1597. o.Add(s, 0);
  1598. }
  1599. }
  1600. if (Str.StrCmpi(name, "CMD"))
  1601. {
  1602. ret = str.Substring(i + 5).Trim();
  1603. }
  1604. else
  1605. {
  1606. tmp = tmp.Substring(0, i);
  1607. }
  1608. }
  1609. if (ret == null)
  1610. {
  1611. string[] t = Str.ParseCmdLine(tmp);
  1612. if (t != null)
  1613. {
  1614. for (i = 0; i < t.Length; i++)
  1615. {
  1616. string token = t[i];
  1617. if ((token[0] == '-' && token[1] != '-') ||
  1618. (Str.StrCmpi(token, "--help")) ||
  1619. (token[0] == '/' && token[1] != '/'))
  1620. {
  1621. int j;
  1622. if (Str.StrCmpi(token, "--help"))
  1623. {
  1624. token = token.Substring(1);
  1625. }
  1626. j = Str.SearchStr(token, ":", 0, false);
  1627. if (j == -1)
  1628. {
  1629. j = Str.SearchStr(token, "=", 0, false);
  1630. }
  1631. if (j != -1)
  1632. {
  1633. string tmp2;
  1634. string a;
  1635. tmp2 = token;
  1636. if (tmp2.Length >= j)
  1637. {
  1638. tmp2 = tmp2.Substring(0, j);
  1639. }
  1640. a = tmp2.Substring(1);
  1641. if (o != null)
  1642. {
  1643. if (o.ContainsKey(a) == false)
  1644. {
  1645. o.Add(a, 0);
  1646. }
  1647. }
  1648. if (tmp2.Length >= 1 && Str.StrCmpi(name, tmp2.Substring(1)))
  1649. {
  1650. if (ret == null)
  1651. {
  1652. ret = token.Substring(j + 1);
  1653. }
  1654. }
  1655. }
  1656. else
  1657. {
  1658. string a = token.Substring(1);
  1659. if (o != null)
  1660. {
  1661. if (o.ContainsKey(a) == false)
  1662. {
  1663. o.Add(a, 0);
  1664. }
  1665. if (Str.StrCmpi(name, token.Substring(1)))
  1666. {
  1667. if (ret == null)
  1668. {
  1669. ret = "";
  1670. }
  1671. }
  1672. }
  1673. }
  1674. }
  1675. else
  1676. {
  1677. if (name == null)
  1678. {
  1679. if (ret == null)
  1680. {
  1681. if (token.StartsWith("--"))
  1682. {
  1683. ret = token.Substring(1);
  1684. }
  1685. else if (token.StartsWith("//"))
  1686. {
  1687. ret = token.Substring(1);
  1688. }
  1689. else
  1690. {
  1691. ret = token;
  1692. }
  1693. }
  1694. }
  1695. }
  1696. }
  1697. }
  1698. }
  1699. if (o != null)
  1700. {
  1701. List<string> t = new List<string>();
  1702. int j;
  1703. for (j = 0; j < o.Count; j++)
  1704. {
  1705. t.Add(o.Keys[j]);
  1706. }
  1707. paramList = t.ToArray();
  1708. }
  1709. if (ret != null)
  1710. {
  1711. if (Str.StrCmpi(ret, "none") || Str.StrCmpi(ret, "null"))
  1712. {
  1713. ret = "";
  1714. }
  1715. }
  1716. return ret;
  1717. }
  1718. public static ConsoleService NewLocalConsoleService()
  1719. {
  1720. return NewLocalConsoleService(null, null);
  1721. }
  1722. public static ConsoleService NewLocalConsoleService(string outFileName)
  1723. {
  1724. return NewLocalConsoleService(null, outFileName);
  1725. }
  1726. public static ConsoleService NewLocalConsoleService(string inFileName, string outFileName)
  1727. {
  1728. IO in_io = null, out_io = null;
  1729. ConsoleService c = new ConsoleService();
  1730. int old_size = 0;
  1731. c.consoleType = ConsoleType.Local;
  1732. c.free = new ConsoleFreeDelegate(c.localFree);
  1733. c.readLine = new ConsoleReadLineDelegate(c.localReadLine);
  1734. c.readPassword = new ConsoleReadPasswordDelegate(c.localReadPassword);
  1735. c.write = new ConsoleWriteDelegate(c.localWrite);
  1736. c.getWidth = new ConsoleGetWidthDelegate(c.localGetWidth);
  1737. if (Str.IsEmptyStr(inFileName) == false)
  1738. {
  1739. try
  1740. {
  1741. in_io = IO.FileOpen(inFileName, false);
  1742. }
  1743. catch
  1744. {
  1745. c.write(Str.FormatC(Resources.CON_INFILE_ERROR, inFileName));
  1746. return null;
  1747. }
  1748. c.write(Str.FormatC(Resources.CON_INFILE_START, inFileName));
  1749. }
  1750. if (Str.IsEmptyStr(outFileName) == false)
  1751. {
  1752. try
  1753. {
  1754. out_io = IO.FileCreate(outFileName);
  1755. }
  1756. catch
  1757. {
  1758. c.write(Str.FormatC(Resources.CON_OUTFILE_ERROR, outFileName));
  1759. if (in_io != null)
  1760. {
  1761. in_io.Close();
  1762. }
  1763. return null;
  1764. }
  1765. c.write(Str.FormatC(Resources.CON_OUTFILE_START, outFileName));
  1766. }
  1767. c.inFile = in_io;
  1768. c.outFile = out_io;
  1769. c.win32_OldConsoleWidth = old_size;
  1770. if (in_io != null)
  1771. {
  1772. byte[] data = in_io.ReadAll();
  1773. c.inBuf = new Buf(data);
  1774. }
  1775. Con.SetConsoleService(c);
  1776. return c;
  1777. }
  1778. void localFree()
  1779. {
  1780. if (inFile != null)
  1781. {
  1782. inFile.Close();
  1783. inFile = null;
  1784. }
  1785. if (outFile != null)
  1786. {
  1787. outFile.Close();
  1788. outFile = null;
  1789. }
  1790. }
  1791. int localGetWidth()
  1792. {
  1793. int ret = Console.WindowWidth;
  1794. if (ret <= 0)
  1795. {
  1796. ret = 1;
  1797. }
  1798. return ret;
  1799. }
  1800. string localReadLine(string prompt, bool noFile)
  1801. {
  1802. string ret;
  1803. if (prompt == null)
  1804. {
  1805. prompt = ">";
  1806. }
  1807. writeOutFile(prompt, false);
  1808. if (noFile == false && inBuf != null)
  1809. {
  1810. ret = readNextFromInFile();
  1811. if (ret != null)
  1812. {
  1813. Console.Write(prompt);
  1814. Console.WriteLine(ret);
  1815. }
  1816. }
  1817. else
  1818. {
  1819. Console.Write(prompt);
  1820. ret = Console.ReadLine();
  1821. if (ret != null)
  1822. {
  1823. if (ret.IndexOf((char)0x04) != -1 || ret.IndexOf((char)0x1a) != -1)
  1824. {
  1825. ret = null;
  1826. }
  1827. }
  1828. }
  1829. if (ret != null)
  1830. {
  1831. writeOutFile(ret, true);
  1832. }
  1833. else
  1834. {
  1835. writeOutFile("[EOF]", true);
  1836. }
  1837. return ret;
  1838. }
  1839. string localReadPassword(string prompt)
  1840. {
  1841. if (prompt == null)
  1842. {
  1843. prompt = "Password>";
  1844. }
  1845. Console.Write(prompt);
  1846. writeOutFile(prompt, false);
  1847. string tmp = Str.PasswordPrompt();
  1848. if (tmp != null)
  1849. {
  1850. writeOutFile("********", true);
  1851. return tmp;
  1852. }
  1853. return null;
  1854. }
  1855. bool localWrite(string str)
  1856. {
  1857. Console.Write("{0}{1}",
  1858. str,
  1859. (str.EndsWith("\n") ? "" : "\n"));
  1860. writeOutFile(str, true);
  1861. return true;
  1862. }
  1863. string readNextFromInFile()
  1864. {
  1865. if (inBuf == null)
  1866. {
  1867. return null;
  1868. }
  1869. while (true)
  1870. {
  1871. string str = inBuf.ReadNextLineAsString();
  1872. if (str == null)
  1873. {
  1874. return null;
  1875. }
  1876. str = str.Trim();
  1877. if (Str.IsEmptyStr(str) == false)
  1878. {
  1879. return str;
  1880. }
  1881. }
  1882. }
  1883. void writeOutFile(string str, bool addLastCrlf)
  1884. {
  1885. if (outFile != null)
  1886. {
  1887. string tmp = Str.NormalizeCrlf(str);
  1888. outFile.Write(Str.Utf8Encoding.GetBytes(str));
  1889. if (str.EndsWith("\n") == false && addLastCrlf)
  1890. {
  1891. outFile.Write(Str.Utf8Encoding.GetBytes(Env.NewLine));
  1892. }
  1893. outFile.Flush();
  1894. }
  1895. }
  1896. }
  1897. }