私はbashシェルの "inside" C#プログラムを使う必要があります。インタラクティブモードでのユーザの入力を模倣し、cygwinコマンドを実行したい。C#プログラムの中でbash(cygwin)を使う
私はstdin、stout、およびstdエラーを実行するプロセスを作成しましたが、ttyを動作させるには、bashプロセスを開始し、入出力をリダイレクトするサンプルコードが必要です。
問題は私にttyデバイスがないことです。私がttyコマンドまたはにsttyコマンドを実行しようと私はcygwinの実行とエコーとのstty -echoを無効にしますが、この私を行うにする必要があり、これはpsi.UseShellExecute = false;
から発生していると思うエラー応答に
tty - not a tty
stty - Inappropriate ioctl for device
を受け取りますttyデバイスが必要です。どのように私はttyデバイスとcygwinのbashシェルを作成し、stdinをリダイレクトすると、エラーとエラー?
1)何が欠けていますか?
あなたが正しく...あなたは電子を探すために必要なイベントを処理していない:質問に答えるためにhttp://www.codeproject.com/KB/IP/sharpssh.aspx
:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace shartCygwin
{
class Program
{
private static Queue<string> ResponseQueue = null;
private static ManualResetEvent ResponseEvent = null;
static void Main(string[] args)
{
ResponseQueue = new Queue<string>();
ResponseEvent = new ManualResetEvent(false);
Process bashProcess = new Process();
bashProcess.StartInfo.FileName = "C:\\cygwin\\bin\\bash.exe";
bashProcess.StartInfo.Arguments = "--login -i ";
bashProcess.StartInfo.WorkingDirectory = "C:\\cygwin\\bin";
bashProcess.StartInfo.EnvironmentVariables["CYGWIN"] = "tty";
bashProcess.StartInfo.RedirectStandardError = true;
bashProcess.StartInfo.RedirectStandardInput = true;
bashProcess.StartInfo.RedirectStandardOutput = true;
bashProcess.StartInfo.CreateNoWindow = true;
bashProcess.StartInfo.UseShellExecute = false;
bashProcess.StartInfo.ErrorDialog = false;
bashProcess.Start();
DataReceivedEventHandler errorEventHandler = new DataReceivedEventHandler(ErrorDataReceived);
DataReceivedEventHandler outEventHandler = new DataReceivedEventHandler(OutDataReceived);
bashProcess.OutputDataReceived += outEventHandler;
bashProcess.ErrorDataReceived += errorEventHandler;
bashProcess.BeginErrorReadLine();
bashProcess.BeginOutputReadLine();
while(true)
{
Thread.Sleep(1000);
}
}
static void ErrorDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
try
{
lock (ResponseQueue)
{
Console.WriteLine(dataReceivedEventArgs.Data);
ResponseQueue.Enqueue(dataReceivedEventArgs.Data);
ResponseEvent.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.Data);
}
}
static void OutDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
try
{
lock (ResponseQueue)
{
Console.WriteLine(dataReceivedEventArgs.Data);
ResponseQueue.Enqueue(dataReceivedEventArgs.Data);
ResponseEvent.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.Data);
}
}
}
}
あなたのプログラムを実行しましたが、「不適切なioctl for device」エラーが表示されても、そのあとでcygwin bashプロンプトが表示されます。何が問題なのですか?私はCYGWIN = tty行を削除して、制御コードが表示された状態でte出力がrawであることを確認しました。 –
@Hemlock - Kevin Markの答えに基づく鍵は、エコーを使うかどうかを決めることだと思うので、エコーをオフにすれば本当に問題になります。 CYGWINをTTYを使用するように騙そうとしていますか? – Peter