private static DeviceEntry LoadFromConfig(string deviceName, string configFile) { if (!File.Exists(configFile)) { return(null); } using (ScadaReader sr = new ScadaReader(configFile)) { SectionType secType = SectionType.None; string line = null; string key = null; IValue value = null; ReadLineResult result = sr.ReadLine(out secType, out line, out key, out value); // Dictionary<string, string> config = new Dictionary<string, string>(); DeviceEntry entry = new DeviceEntry(); while (result == ReadLineResult.OK) { result = sr.ReadLine(out secType, out line, out key, out value); if (secType == SectionType.KeyWithStringValue) { entry[key] = value; } } DirectoryInfo di = Directory.GetParent(configFile); string devicePath = di.FullName; // Path entry[DeviceEntry.Path] = new StringValue(devicePath); entry[DeviceEntry.Identity] = new StringValue(deviceName); return(entry); } }
private string ReadChoiceResponse(out ReadLineResult result) { result = ReadLineResult.endedOnEnter; return(InternalTestHooks.ForcePromptForChoiceDefaultOption ? string.Empty : ReadLine( endOnTab: false, initialContent: string.Empty, result: out result, calledFromPipeline: true, transcribeResult: true)); }
private string ReadLineFromConsole(bool endOnTab, string initialContent, bool calledFromPipeline, ref string restOfLine, ref ReadLineResult result) { PreRead(); // Ensure that we're in the proper line-input mode. #if !UNIX ConsoleHandle handle = ConsoleControl.GetConioDeviceHandle(); ConsoleControl.ConsoleModes m = ConsoleControl.GetMode(handle); const ConsoleControl.ConsoleModes desiredMode = ConsoleControl.ConsoleModes.LineInput | ConsoleControl.ConsoleModes.EchoInput | ConsoleControl.ConsoleModes.ProcessedInput; if ((m & desiredMode) != desiredMode || (m & ConsoleControl.ConsoleModes.MouseInput) > 0) { m &= ~ConsoleControl.ConsoleModes.MouseInput; m |= desiredMode; ConsoleControl.SetMode(handle, m); } #endif // If more characters are typed than you asked, then the next call to ReadConsole will return the // additional characters beyond those you requested. // // If input is terminated with a tab key, then the buffer returned will have a tab (ascii 0x9) at the // position where the tab key was hit. If the user has arrowed backward over existing input in the line // buffer, the tab will overwrite whatever character was in that position. That character will be lost in // the input buffer, but since we echo each character the user types, it's still in the active screen buffer // and we can read the console output to get that character. // // If input is terminated with an enter key, then the buffer returned will have ascii 0x0D and 0x0A // (Carriage Return and Line Feed) as the last two characters of the buffer. // // If input is terminated with a break key (Ctrl-C, Ctrl-Break, Close, etc.), then the buffer will be // the empty string. #if UNIX // For Unix systems, we implement a basic readline loop around Console.ReadKey(), that // supports backspace, arrow keys, Ctrl-C, and Ctrl-D. This readline is only used for // interactive prompts (like Read-Host), otherwise it is assumed that PSReadLine is // available. Therefore this explicitly does not support history or tab completion. bool treatControlCAsInput = Console.TreatControlCAsInput; try { ConsoleKeyInfo keyInfo; string s = ""; int index = 0; int cursorLeft = Console.CursorLeft; int cursorCurrent = cursorLeft; bool insertMode = true; Console.TreatControlCAsInput = true; #else _rawui.ClearKeyCache(); uint keyState = 0; string s = ""; #endif do { #if UNIX keyInfo = Console.ReadKey(true); #else s += ConsoleControl.ReadConsole(handle, initialContent, maxInputLineLength, endOnTab, out keyState); Dbg.Assert(s != null, "s should never be null"); #endif #if UNIX // Handle Ctrl-C ending input if (keyInfo.Key == ConsoleKey.C && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control)) #else if (s.Length == 0) #endif { result = ReadLineResult.endedOnBreak; s = null; if (calledFromPipeline) { // make sure that the pipeline that called us is stopped throw new PipelineStoppedException(); } break; } #if UNIX if (keyInfo.Key == ConsoleKey.Enter) #else if (s.EndsWith(Crlf, StringComparison.CurrentCulture)) #endif { result = ReadLineResult.endedOnEnter; #if UNIX // We're intercepting characters, so we need to echo the newline Console.Out.WriteLine(); #else s = s.Remove(s.Length - Crlf.Length); #endif break; } #if UNIX if (keyInfo.Key == ConsoleKey.Tab) { // This is unsupported continue; } #else int i = s.IndexOf(Tab, StringComparison.CurrentCulture); if (endOnTab && i != -1) { // then the tab we found is the completion character. bit 0x10 is set if the shift key was down // when the key was hit. if ((keyState & 0x10) == 0) { result = ReadLineResult.endedOnTab; } else if ((keyState & 0x10) > 0) { result = ReadLineResult.endedOnShiftTab; } else { // do nothing: leave the result state as it was. This is the circumstance when we've have to // do more than one iteration and the input ended on a tab or shift-tab, or the user hit // enter, or the user hit ctrl-c } // also clean up the screen -- if the cursor was positioned somewhere before the last character // in the input buffer, then the characters from the tab to the end of the buffer need to be // erased. int leftover = RawUI.LengthInBufferCells(s.Substring(i + 1)); if (leftover > 0) { Coordinates c = RawUI.CursorPosition; // before cleaning up the screen, read the active screen buffer to retrieve the character that // is overridden by the tab char charUnderCursor = GetCharacterUnderCursor(c); Write(new string(' ', leftover)); RawUI.CursorPosition = c; restOfLine = s[i] + (charUnderCursor + s.Substring(i + 1)); } else { restOfLine += s[i]; } s = s.Remove(i); break; } #endif #if UNIX if (keyInfo.Key == ConsoleKey.Backspace) { if (index > 0) { int length = s.Length; s = s.Remove(index - 1, 1); index--; cursorCurrent = Console.CursorLeft; Console.CursorLeft = cursorLeft; Console.Out.Write(s.PadRight(length)); Console.CursorLeft = cursorCurrent - 1; } continue; } if (keyInfo.Key == ConsoleKey.Delete || (keyInfo.Key == ConsoleKey.D && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))) { if (index < s.Length) { int length = s.Length; s = s.Remove(index, 1); cursorCurrent = Console.CursorLeft; Console.CursorLeft = cursorLeft; Console.Out.Write(s.PadRight(length)); Console.CursorLeft = cursorCurrent; } continue; } if (keyInfo.Key == ConsoleKey.LeftArrow || (keyInfo.Key == ConsoleKey.B && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))) { if (Console.CursorLeft > cursorLeft) { Console.CursorLeft--; index--; } continue; } if (keyInfo.Key == ConsoleKey.RightArrow || (keyInfo.Key == ConsoleKey.F && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))) { if (Console.CursorLeft < cursorLeft + s.Length) { Console.CursorLeft++; index++; } continue; } if (keyInfo.Key == ConsoleKey.UpArrow || keyInfo.Key == ConsoleKey.DownArrow || keyInfo.Key == ConsoleKey.PageUp || keyInfo.Key == ConsoleKey.PageDown) { // Arrow/Page Up/down is unimplemented, so fail gracefully continue; } if (keyInfo.Key == ConsoleKey.Home || (keyInfo.Key == ConsoleKey.A && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))) { Console.CursorLeft = cursorLeft; index = 0; continue; } if (keyInfo.Key == ConsoleKey.End || (keyInfo.Key == ConsoleKey.E && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))) { Console.CursorLeft = cursorLeft + s.Length; index = s.Length; continue; } if (keyInfo.Key == ConsoleKey.Escape) { Console.CursorLeft = cursorLeft; index = s.Length; s = ""; continue; } if (keyInfo.Key == ConsoleKey.Insert) { // Toggle insert/overwrite mode insertMode = !insertMode; continue; } if (Char.IsControl(keyInfo.KeyChar)) { // blacklist control characters continue; } // Modify string if (!insertMode) // then overwrite mode { s = s.Remove(index, 1); } s = s.Insert(index, keyInfo.KeyChar.ToString()); index++; // Redisplay string cursorCurrent = Console.CursorLeft; Console.CursorLeft = cursorLeft; Console.Out.Write(s); Console.CursorLeft = cursorCurrent + 1; #endif } while (true); Dbg.Assert( (s == null && result == ReadLineResult.endedOnBreak) || (s != null && result != ReadLineResult.endedOnBreak), "s should only be null if input ended with a break"); return s; #if UNIX } finally { Console.TreatControlCAsInput = treatControlCAsInput; } #endif }
/// <summary> /// /// Reads a line of input from the console. Returns when the user hits enter, a break key, a break event occurs. In /// the case that stdin has been redirected, reads from the stdin stream instead of the console. /// /// </summary> /// <param name="endOnTab"> /// /// true to end input when the user hits the tab or shift-tab keys, false to only end on the enter key (or a break /// event). Ignored if not reading from the console device. /// /// </param> /// <param name="initialContent"> /// /// The initial contents of the input buffer. Nice if you want to have a default result. Ignored if not reading from the /// console device. /// /// </param> /// <param name="result"> /// /// Receives an enum value indicating how input was ended. /// /// </param> /// <param name="calledFromPipeline"> /// /// TBD /// /// </param> /// <param name="transcribeResult"> /// /// true to include the results in any transcription that might be happening. /// /// </param> /// /// <returns> /// /// The string read from either the console or the stdin stream. null if: /// - stdin was read and EOF was reached on the stream, or /// - the console was read, and input was terminated with Ctrl-C, Ctrl-Break, or Close. /// /// </returns> /// <exception cref="HostException"> /// /// If Win32's SetConsoleMode fails /// OR /// Win32's ReadConsole fails /// OR /// obtaining information about the buffer failed /// OR /// Win32's SetConsoleCursorPosition failed /// /// </exception> internal string ReadLine(bool endOnTab, string initialContent, out ReadLineResult result, bool calledFromPipeline, bool transcribeResult) { result = ReadLineResult.endedOnEnter; // If the test hook is set, read from it. if (s_h != null) return s_h.ReadLine(); string restOfLine = null; string s = ReadFromStdin ? ReadLineFromFile(initialContent) : ReadLineFromConsole(endOnTab, initialContent, calledFromPipeline, ref restOfLine, ref result); if (transcribeResult) { PostRead(s); } else { PostRead(); } if (restOfLine != null) s += restOfLine; return s; }
private void LoadDevicesInfo(string installPath) { this.dict.Clear(); string[] deviceConfigPaths = Directory.GetDirectories(ConfigPath.GetConfigFilePath(DevicePath)); foreach (string devicePath in deviceConfigPaths) { string deviceName = DirectoryName(devicePath); if (deviceName.StartsWith("!") || deviceName.StartsWith(".")) { continue; } string deviceKey = deviceName.ToLower(); DevicesInfo di = null; if (!dict.ContainsKey(deviceKey)) { di = new DevicesInfo() { Name = deviceName }; di.Versions = new List <string>(); dict.Add(deviceKey, di); } else { di = dict[deviceKey]; } string displayConfig = devicePath + "\\display.cfg"; if (File.Exists(displayConfig)) { using (ScadaReader sr = new ScadaReader(displayConfig)) { // TODO: Xml Reader parse the whole file. // And retrieve the resulr, no need to loop reading line // SectionType secType = SectionType.None; string line = null; string key = null; IValue value = null; ReadLineResult result = sr.ReadLine(out secType, out line, out key, out value); while (result == ReadLineResult.OK) { if (key.ToLower() == "name") { di.DisplayName = value.ToString(); } result = sr.ReadLine(out secType, out line, out key, out value); } } } string[] versionPaths = Directory.GetDirectories(devicePath); foreach (string versionPath in versionPaths) { string version = DirectoryName(versionPath); di.Versions.Add(version); } } }