www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/09/23/17:04:46

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=0.6 required=5.0 tests=BAYES_50,J_CHICKENPOX_55
X-Spam-Check-By: sourceware.org
From: "amir knippel" <amir AT spinux DOT com>
To: <cygwin AT cygwin DOT com>
Subject: Using bash(cygwin) inside C# program
Date: Thu, 24 Sep 2009 00:04:23 +0300
Message-ID: <001e01ca3c91$70f661c0$52e32540$@com>
MIME-Version: 1.0
x-cr-hashedpuzzle: AXu0 AYSj Ar3k Asby AuMz CcmD Chy9 CmSw CuiU DWJG EB2j FGe9 GucB HANK HhZj HyOy;1;YwB5AGcAdwBpAG4AQABjAHkAZwB3AGkAbgAuAGMAbwBtAA==;Sosha1_v1;7;{4B21EEE3-5422-42D4-B864-F38249D08EEB};YQBtAGkAcgBAAHMAcABpAG4AdQB4AC4AYwBvAG0A;Wed, 23 Sep 2009 21:04:19 GMT;VQBzAGkAbgBnACAAYgBhAHMAaAAoAGMAeQBnAHcAaQBuACkAIABpAG4AcwBpAGQAZQAgAEMAIwAgAHAAcgBvAGcAcgBhAG0A
x-cr-puzzleid: {4B21EEE3-5422-42D4-B864-F38249D08EEB}
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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 can=92t 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=20
tty - not a tty=20
stty - Inappropriate ioctl for device

i think the this is caused from psi.UseShellExecute =3D false;=20
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
{
=A0 =A0 class Program
=A0 =A0 {
=A0 =A0 =A0 =A0 private static Queue<string> ResponseQueue =3D null;
=A0 =A0 =A0 =A0 private static ManualResetEvent ResponseEvent =3D null;

=A0 =A0 =A0 =A0 static void Main(string[] args)
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 ResponseQueue =3D new Queue<string>();
=A0 =A0 =A0 =A0 =A0 =A0 ResponseEvent =3D new ManualResetEvent(false);

=A0 =A0 =A0 =A0 =A0 =A0 Process bashProcess =3D new Process();

=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.FileName =3D "C:\\cygwin\\bin=
\\bash.exe";=20
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.Arguments =3D "--login -i "; =
=A0
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.WorkingDirectory =3D "C:\\cyg=
win\\bin";

=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.EnvironmentVariables["CYGWIN"=
] =3D "tty";

=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.RedirectStandardError =3D tru=
e;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.RedirectStandardInput =3D tru=
e;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.RedirectStandardOutput =3D tr=
ue;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.CreateNoWindow =3D true;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.UseShellExecute =3D false;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.StartInfo.ErrorDialog =3D false;

=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.Start();

=A0 =A0 =A0 =A0 =A0 =A0 DataReceivedEventHandler errorEventHandler =3D new
DataReceivedEventHandler(ErrorDataReceived);
=A0 =A0 =A0 =A0 =A0 =A0 DataReceivedEventHandler outEventHandler =3D new
DataReceivedEventHandler(OutDataReceived);
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.OutputDataReceived +=3D outEventHandler;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.ErrorDataReceived +=3D errorEventHandle=
r;
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.BeginErrorReadLine();
=A0 =A0 =A0 =A0 =A0 =A0 bashProcess.BeginOutputReadLine();

=A0 =A0 =A0 =A0 =A0 =A0 while(true)
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Thread.Sleep(1000);
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 }

=A0 =A0 =A0 =A0 static void ErrorDataReceived(object sender, DataReceivedEv=
entArgs
dataReceivedEventArgs)
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 try
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 lock (ResponseQueue)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine(dataReceivedEvent=
Args.Data);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ResponseQueue.Enqueue(dataReceivedE=
ventArgs.Data);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ResponseEvent.Set();
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0 =A0 catch (Exception e)
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine(e.Data);
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 }

=A0 =A0 =A0 =A0 static void OutDataReceived(object sender, DataReceivedEven=
tArgs
dataReceivedEventArgs)
=A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 try
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 lock (ResponseQueue)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine(dataReceivedEvent=
Args.Data);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ResponseQueue.Enqueue(dataReceivedE=
ventArgs.Data);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ResponseEvent.Set();
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0 =A0 catch (Exception e)
=A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Console.WriteLine(e.Data);
=A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 }
=A0 =A0 }
}



--
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019