Program.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Principal;
  5. using System.Reflection;
  6. using System.Text;
  7. using DiskAccessLibrary.LogicalDiskManager;
  8. using DiskAccessLibrary;
  9. using Utilities;
  10. namespace ISCSIConsole
  11. {
  12. partial class Program
  13. {
  14. public static bool m_debug = false;
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine("iSCSI Console v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
  18. Console.WriteLine("Author: Tal Aloni (tal.aloni.il@gmail.com)");
  19. MainLoop();
  20. }
  21. public static void MainLoop()
  22. {
  23. bool exit = false;
  24. while (true)
  25. {
  26. if (m_debug)
  27. {
  28. exit = ProcessCommand();
  29. }
  30. else
  31. {
  32. try
  33. {
  34. exit = ProcessCommand();
  35. }
  36. catch (Exception ex)
  37. {
  38. Console.WriteLine("Unhandled exception: " + ex.ToString());
  39. }
  40. }
  41. if (exit)
  42. {
  43. break;
  44. }
  45. }
  46. }
  47. /// <returns>true to exit</returns>
  48. public static bool ProcessCommand()
  49. {
  50. Console.WriteLine();
  51. Console.Write("iSCSI> ");
  52. string command = Console.ReadLine();
  53. string[] args = GetCommandArgsIgnoreEscape(command);
  54. bool exit = false;
  55. if (args.Length > 0)
  56. {
  57. string commandName = args[0];
  58. switch (commandName.ToLower())
  59. {
  60. case "attach":
  61. AttachCommand(args);
  62. break;
  63. case "create":
  64. CreateCommand(args);
  65. break;
  66. case "detail":
  67. DetailCommand(args);
  68. break;
  69. case "exit":
  70. exit = true;
  71. if (m_server != null)
  72. {
  73. m_server.Stop();
  74. m_server = null;
  75. }
  76. break;
  77. case "help":
  78. {
  79. HelpCommand(args);
  80. break;
  81. }
  82. case "list":
  83. ListCommand(args);
  84. break;
  85. case "offline":
  86. OfflineCommand(args);
  87. break;
  88. case "online":
  89. OnlineCommand(args);
  90. break;
  91. case "select":
  92. SelectCommand(args);
  93. break;
  94. case "set":
  95. SetCommand(args);
  96. break;
  97. case "start":
  98. StartCommand(args);
  99. break;
  100. case "stop":
  101. StopCommand(args);
  102. break;
  103. default:
  104. Console.WriteLine("Invalid command. use the 'HELP' command to see the list of commands.");
  105. break;
  106. }
  107. }
  108. return exit;
  109. }
  110. public static KeyValuePairList<string, string> ParseParameters(string[] args, int start)
  111. {
  112. KeyValuePairList<string, string> result = new KeyValuePairList<string, string>();
  113. for (int index = start; index < args.Length; index++)
  114. {
  115. string[] pair = args[index].Split('=');
  116. if (pair.Length >= 2)
  117. {
  118. string key = pair[0].ToLower(); // we search by the key, so it should be set to lowercase
  119. string value = pair[1];
  120. value = Unquote(value);
  121. result.Add(key, value);
  122. }
  123. else
  124. {
  125. result.Add(pair[0].ToLower(), String.Empty);
  126. }
  127. }
  128. return result;
  129. }
  130. /// <summary>
  131. /// Make sure all given parameters are allowed
  132. /// </summary>
  133. public static bool VerifyParameters(KeyValuePairList<string, string> parameters, params string[] allowedKeys)
  134. {
  135. List<string> allowedList = new List<string>(allowedKeys);
  136. List<string> keys = parameters.Keys;
  137. foreach(string key in keys)
  138. {
  139. if (!allowedList.Contains(key))
  140. {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. private static int IndexOfUnquotedSpace(string str)
  147. {
  148. return IndexOfUnquotedSpace(str, 0);
  149. }
  150. private static int IndexOfUnquotedSpace(string str, int startIndex)
  151. {
  152. return QuotedStringUtils.IndexOfUnquotedChar(str, ' ', startIndex);
  153. }
  154. public static string Unquote(string str)
  155. {
  156. string quote = '"'.ToString();
  157. if (str.StartsWith(quote) && str.EndsWith(quote))
  158. {
  159. return str.Substring(1, str.Length - 2);
  160. }
  161. else
  162. {
  163. return str;
  164. }
  165. }
  166. private static string[] GetCommandArgsIgnoreEscape(string commandLine)
  167. {
  168. List<string> argsList = new List<string>();
  169. int endIndex = IndexOfUnquotedSpace(commandLine);
  170. int startIndex = 0;
  171. while (endIndex != -1)
  172. {
  173. int length = endIndex - startIndex;
  174. string nextArg = commandLine.Substring(startIndex, length);
  175. nextArg = Unquote(nextArg);
  176. argsList.Add(nextArg);
  177. startIndex = endIndex + 1;
  178. endIndex = IndexOfUnquotedSpace(commandLine, startIndex);
  179. }
  180. string lastArg = commandLine.Substring(startIndex);
  181. lastArg = Unquote(lastArg);
  182. if (lastArg != String.Empty)
  183. {
  184. argsList.Add(lastArg);
  185. }
  186. return argsList.ToArray();
  187. }
  188. }
  189. }