Re: NSTask troubles
Re: NSTask troubles
- Subject: Re: NSTask troubles
- From: John Stiles <email@hidden>
- Date: Thu, 12 Jul 2007 20:20:58 -0700
On Jul 12, 2007, at 6:01 PM, John Stiles wrote:
Well, I got this far but it's not working at all.
@interface NSTask (PseudoTTY)
- (void) usePseudoTTY;
@end
@implementation NSTask (PseudoTTY)
- (void) usePseudoTTY
{
int masterFD, slaveFD;
if( 0 != openpty( &masterFD, &slaveFD, NULL, NULL, NULL ) )
{
ASSERT( !"unable to start pseudo-tty" );
return;
}
[self setStandardInput:[[[NSFileHandle alloc]
initWithFileDescriptor:slaveFD] autorelease]];
[self setStandardOutput:[[[NSFileHandle alloc]
initWithFileDescriptor:masterFD] autorelease]];
}
@end
NS_DURING
[atosTask setLaunchPath:@"/usr/bin/atos"];
[atosTask setArguments:[NSArray arrayWithObjects:
@"-o",
[[NSFileManager defaultManager]
stringWithFileSystemRepresentation:imagePath strlen(imagePath)],
NULL]];
[atosTask usePseudoTTY];
[atosTask launch];
NS_HANDLER
// fail gracefully
return;
NS_ENDHANDLER
I'm getting back the string I write in, with one change—'\n' gets
converted to '\r\n'.
I tried switching around the slave and master but that didn't work
either.
OK, I've nailed it, but what a pain in the ass! forkpty isn't
documented well anywhere that I could find (on the web at least). And
in its default state, the pseudo-TTY has all sorts of undesirable
behaviors like echoing, and \n becoming \r\n.
For the archives, here's what worked:
int fd;
switch( pid_t pid = forkpty( &fd, NULL, NULL, NULL ) )
{
case -1: // error
// fail gracefully
return;
case 0: // forked process
execl( "/usr/bin/atos", "/usr/bin/atos", "-o", imagePath, NULL );
abort(); // should not reach
default: // master process
pid; // is unused
break;
}
// Disable echo and CR conversion.
termios buf = {};
tcgetattr( fd, &buf );
buf.c_oflag &= ~ONLCR;
buf.c_lflag &= ~ECHO;
tcsetattr( fd, TCSANOW, &buf );
char atosResult[8192];
if( !WriteLine( atosFD, FormatString( "0x%llx\n", uint64(pc) ) ) ||
!ReadLine( atosFD, atosResult, sizeof(atosResult) ) )
{
// fail gracefully
return;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden