Using bash(cygwin) inside C# program
amir knippel
amir@spinux.com
Wed Sep 23 21:04:00 GMT 2009
Hey everyone,
i need to use bash shell "inside" C# program. I want to mimic user typing in
interactive mode and running cygwin commands.
i created a process that runs bash and redirect stdin,stout and std error
but I cant get tty to work attached is a sample code that starts bash
process and redirect the input/output.
the problem is that i don't have tty device. if i try to run tty command or
stty command i receive error response
tty - not a tty
stty - Inappropriate ioctl for device
i think the this is caused from psi.UseShellExecute = false;
i need to run cygwin and disable echo with stty -echo but to do this i need
a tty device. how can i create a cygwin bash shell with tty device and
redirect the stdin, out and error ?
Here is simplified code:
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);
}
}
}
}
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
More information about the Cygwin
mailing list