private string ProcessUsingScriptFile(string clipboardText, string scriptFileDirectoryFullPath, ComboBox comboBoxScriptFileList, bool processedResultAutoCopy, bool processedResultAppendToEnd, TextBox textBoxProcessedResult) { string scriptFileName = comboBoxScriptFileList.SelectedValue as string; if (scriptFileName == null) { SetMultilineTextBoxText(textBoxProcessedResult, string.Empty); return(null); } string scriptFileFullPath = Path.Combine(scriptFileDirectoryFullPath, scriptFileName); if (!File.Exists(scriptFileFullPath)) { SetMultilineTextBoxText(textBoxProcessedResult, string.Empty); return(null); } string scriptFileExtension = Path.GetExtension(scriptFileFullPath).TrimStart('.').ToLower(); ScriptInterpreterItem scriptInterpreter = ApplicationService.Config.GetScriptInterpreter(scriptFileExtension); string processedResult = ScriptUtil.CallScriptInterpreter(scriptInterpreter, scriptFileFullPath, clipboardText); if (processedResultAutoCopy) { SetClipboardText(processedResult); } if (processedResultAppendToEnd) { string originalProcessedResult = GetMultilineTextBoxText(textBoxProcessedResult); if (originalProcessedResult == string.Empty) { SetMultilineTextBoxText(textBoxProcessedResult, processedResult); } else { SetMultilineTextBoxText(textBoxProcessedResult, originalProcessedResult + (_textareaLineEndingNormalizeToCrLf ? "\r\n" : Environment.NewLine) + processedResult); } } else { SetMultilineTextBoxText(textBoxProcessedResult, processedResult); } textBoxProcessedResult.Select(textBoxProcessedResult.Text.Length, 0); return(processedResult); }
public static string CallScriptInterpreter(ScriptInterpreterItem scriptInterpreter, string scriptFileFullPath, string inputText) { string programFullPath = ReplaceVariables(scriptInterpreter.ExecutableProgram, scriptFileFullPath); if (!File.Exists(programFullPath)) { throw new Exception(String.Format(I18n._("Executable program \"{0}\" does not exist"), programFullPath)); } string programArguments = ReplaceVariables(scriptInterpreter.CommandLineArguments, scriptFileFullPath); ScriptFileEmbeddedConfig scriptFileEmbeddedConfig = ParseScriptFileEmbeddedConfig(scriptFileFullPath); EncodingType inputEncodingType = StringUtil.GetEfficientEncodingType(scriptInterpreter.InputEncoding, scriptFileEmbeddedConfig.InputEncoding, "inputEncoding"); EncodingType outputEncodingType = StringUtil.GetEfficientEncodingType(scriptInterpreter.OutputEncoding, scriptFileEmbeddedConfig.OutputEncoding, "outputEncoding"); Process process = new Process(); process.StartInfo.FileName = programFullPath; process.StartInfo.Arguments = programArguments; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; if (scriptInterpreter.SetPath != null) { string environmentVariablePath = ReplaceVariables(scriptInterpreter.SetPath, scriptFileFullPath); process.StartInfo.EnvironmentVariables["PATH"] = environmentVariablePath; } int timeout = 3000; StringBuilder rawOutputStringBuilder = new StringBuilder(); AutoResetEvent outputWaitHandle = new AutoResetEvent(false); AutoResetEvent errorWaitHandle = new AutoResetEvent(false); process.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { lock (rawOutputStringBuilder) { rawOutputStringBuilder.AppendLine(e.Data); } } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { lock (rawOutputStringBuilder) { rawOutputStringBuilder.AppendLine(e.Data); } } }; string stdinString = StringUtil.Encode(inputText, inputEncodingType); process.Start(); process.StandardInput.Write(stdinString); process.StandardInput.Close(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { // Process completed. Check process.ExitCode here. } else { // Timed out. } if (!process.HasExited) { process.Kill(); } string outputText = StringUtil.Decode(rawOutputStringBuilder.ToString(), outputEncodingType); return(outputText); }