Console.cs 45 KB

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