• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask troubles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask troubles


  • Subject: Re: NSTask troubles
  • From: John Stiles <email@hidden>
  • Date: Fri, 13 Jul 2007 10:38:59 -0700

On Jul 12, 2007, at 8:35 PM, Chris Suter wrote:

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

I presume you looked at the man page for forkpty.

I sure did. As well as the man pages for forkpty for several other Unices (thanks Google), and several tutorials which implement mini- xterms on Linux.


But note that I said documented well. The man page is not much help if you don't already understand pseudo-TTYs and termios perfectly. And as I said before, I hadn't even heard of these things before yesterday :(

And in its default state, the pseudo-TTY has all sorts of undesirable behaviors like echoing, and \n becoming \r\n.

I was in the process of responding to your earlier post. I was going to say that I think you've got confused about what slave FD and master FD means. You should have set both stdin and stdout for the task to the slave FD and then handled input and output to the task via the master FD. To turn off the echo, passing in a zeroed termios structure would probably suffice.

"Confused" isn't really the right word. I had no idea exactly how slave and master were supposed to be related, because the docs simply do not say. They say "there's a slave and a master" and then they leave you to your own devices.



The following appears to work:

#include <Cocoa/Cocoa.h>
#include <stdlib.h>
#include <util.h>
#include <stdio.h>
#include <string.h>
#include <sys/errno.h>
#include <unistd.h>

@interface NSTask (PseudoTTY)
- (int)usePseudoTTY;
@end

@implementation NSTask (PseudoTTY)

- (int)usePseudoTTY
{
  int masterFD, slaveFD;

  struct termios t;

  bzero (&t, sizeof (t));

if (openpty (&masterFD, &slaveFD, NULL, &t, NULL)) {
fprintf (stderr, "openpty failed %d: %s\n", errno, strerror (errno));
return -1;
}

[self setStandardOutput:[[[NSFileHandle alloc] initWithFileDescriptor:slaveFD] autorelease]];
[self setStandardInput:[[[NSFileHandle alloc] initWithFileDescriptor:slaveFD] autorelease]];


  return masterFD;
}

@end

int main (int argc __attribute__ ((__unused__)),
	  char *argv[] __attribute__ ((__unused__)))
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSTask *atosTask = [[NSTask alloc] init];

  [atosTask setLaunchPath:@"/usr/bin/atos"];
  [atosTask setArguments:[NSArray arrayWithObjects:
			   @"-o",
			   @"test-term",
			   NULL]];
  int masterFD = [atosTask usePseudoTTY];
  [atosTask launch];

  assert (write (masterFD, "1234567890\n", 11) == 11);

  char buffer[1024];

  for (;;) {
    ssize_t amount_read = read (masterFD, buffer, sizeof (buffer));

    if (amount_read < 0) {
      fprintf (stderr, "error %d %s", errno, strerror (errno));
      return -1;
    }

    if (!amount_read)
      return 0;

    write (STDOUT_FILENO, buffer, amount_read);
  }

  [pool release];

  // fail gracefully
  return 0;
}

One thing you could do is create a pipe for input into your task thus bypassing the terminal and avoiding any issues with echo. I'm no expert on any of this so I might be wrong.

Cool, thanks for the info. Although I've already got it going with forkpty and it seems solid so I'm hesitant to change anything :)
FWIW, rather than zeroing out the termios, maybe you want to use cfmakeraw? I experimented with that for a bit but it wasn't helping so I moved on…



_______________________________________________

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


  • Follow-Ups:
    • Re: NSTask troubles
      • From: Charlton Wilbur <email@hidden>
References: 
 >NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: Alastair Houghton <email@hidden>)
 >Re: NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: Alastair Houghton <email@hidden>)
 >Re: NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: Alastair Houghton <email@hidden>)
 >Re: NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: John Stiles <email@hidden>)
 >Re: NSTask troubles (From: Chris Suter <email@hidden>)

  • Prev by Date: Re: What are the official limits of Cocoa when it comes to the length of path names?
  • Next by Date: Re: NSTask troubles
  • Previous by thread: Re: NSTask troubles
  • Next by thread: Re: NSTask troubles
  • Index(es):
    • Date
    • Thread