/// <summary>Runs the data through our presentation handlers.</summary> /// <param name="data">The data to format</param> /// <param name="connection">The connection object this data is going to be sent to</param> /// <param name="sendAllData">Indicates if all data should be sent.</param> /// <returns>A formatted string of data</returns> internal static string FormatData(string data, Connection connection, bool sendAllData) { if (connection.Terminal.UseWordWrap && connection.Terminal.Width > 0) { data = WordWrapHandler.Parse(data, connection.Terminal.Width); } if (connection.Terminal.UseBuffer && !sendAllData) { connection.OutputBuffer.Clear(); string[] temp = BufferHandler.Parse(data); string[] output; bool appendOverflow = false; if (temp.Length > connection.PagingRowLimit) { appendOverflow = true; connection.OutputBuffer.Append(temp); output = connection.OutputBuffer.GetRows(BufferDirection.Forward, connection.PagingRowLimit); } else { output = temp; } data = BufferHandler.Format( output, false, appendOverflow, connection.OutputBuffer.CurrentLocation, connection.OutputBuffer.Length); } if (connection.Terminal.UseANSI) { data = AnsiHandler.Parse(data); } else { // TODO: Remove regex into separate parser. var options = RegexOptions.None; var regex = new Regex(@"<%?\w+[^>]*%>", options); string input = data; string replacement = string.Empty; data = regex.Replace(input, replacement); } if (!connection.Terminal.UseMXP) { // TODO: Remove regex into separate parser. var options = RegexOptions.None; var regex = new Regex(@"</?\w+[^>]*>", options); string input = data; string replacement = string.Empty; data = regex.Replace(input, replacement); } return(data); }
/// <summary>Post-processing as called after negotiation.</summary> public override void AfterNegotiation() { // If we got a successful turn on. if (this.UsState == TelnetOptionState.YES) { this.Connection.Terminal.UseMXP = true; // Request a version tag from the client. this.Connection.Send(AnsiHandler.Parse("<%mxpsecureline%><VERSION>"), true); this.AwaitingVersionResponse = true; // @@@ TODO: Send our mxp headers. } }
// TODO: Optimize more public static string Parse(char[] buffer, int bufferPos, TerminalOptions terminalOptions) { var result = ""; var token = ""; var charFromNewline = 0; var isToken = false; for (var i = 0; i < bufferPos; i++) { // OutputBuffer / OutputParser should be the only place writing ANSI NewLine character sequences, to help guarantee correct Telnet protocol handling. // (Telnet protocol says new lines sent should be CR LF regardless of what OS the server is running on and what OS the client is running on.) Debug.Assert(buffer[i] != '\r' && buffer[i] != '\n', "Output should not include explicit newline characters. Use <%nl%> or OutputBuffer WriteLine functions instead."); if (isToken && buffer[i] == '>') { if (buffer[i - 1] == '%') { isToken = false; token = token.Trim('%'); if (token == "nl") { charFromNewline = 0; token = ""; result += AnsiSequences.NewLine; continue; } if (terminalOptions.UseANSI) { result += AnsiHandler.ConvertCode(token); } token = ""; continue; } } if (!isToken && buffer[i] == '<') { if (buffer[i + 1] == '%') { if (i == 0 || buffer[i - 1] != '\\') { isToken = true; continue; } result = result.Remove(result.Length - 1); } } if (!isToken) { result += buffer[i]; charFromNewline++; if (charFromNewline <= terminalOptions.Width || terminalOptions.UseWordWrap == false) { continue; } var lastSpaceIndex = result.LastIndexOf(' '); if (lastSpaceIndex > 0) { result = result.Remove(lastSpaceIndex, 1); charFromNewline = result.Length - lastSpaceIndex; result = result.Insert(lastSpaceIndex, AnsiSequences.NewLine); } else { result += AnsiSequences.NewLine; charFromNewline = 0; } } else { token += buffer[i]; } } return(result); }
//TODO: Optimize more public static string Parse(char[] buffer, int bufferPos, TerminalOptions terminalOptions) { var result = ""; var token = ""; var charFromNewline = 0; var isToken = false; for (var i = 0; i < bufferPos; i++) { if (isToken && buffer[i] == '>') { if (buffer[i - 1] == '%') { isToken = false; token = token.Trim('%'); if (token == "nl") { charFromNewline = 0; token = ""; result += AnsiSequences.NewLine; continue; } if (terminalOptions.UseANSI) { result += AnsiHandler.ConvertCode(token); } token = ""; continue; } } if (!isToken && buffer[i] == '<') { if (buffer[i + 1] == '%') { if (i == 0 || buffer[i - 1] != '\\') { isToken = true; continue; } result = result.Remove(result.Length - 1); } } if (!isToken) { result += buffer[i]; charFromNewline++; if (charFromNewline <= terminalOptions.Width || terminalOptions.UseWordWrap == false) { continue; } var lastSpaceIndex = result.LastIndexOf(' '); if (lastSpaceIndex > 0) { result = result.Remove(lastSpaceIndex, 1); charFromNewline = result.Length - lastSpaceIndex; result = result.Insert(lastSpaceIndex, AnsiSequences.NewLine); } else { result += AnsiSequences.NewLine; charFromNewline = 0; } } else { token += buffer[i]; } } return(result); }