AMShellWrapper将数据发送到正在运行的任务

时间:2010-08-22 19:17:38

标签: objective-c nstask

我正在将AMShellWrapper转换为我自己的运行具有userinput的SH文件的应用程序。因此,我需要将数据发送到正在运行的任务。

有什么想法吗?

利亚

1 个答案:

答案 0 :(得分:0)

你需要一种与PseudoTTY.app一致的方法!

/*
code added to PseudoTTY/PtyView.m

sources:
- PseudoTTY.app, http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip
- charliebot/server.sh, http://sourceforge.net/projects/charliebot/
  (note: modify server.sh to accept complete paths; see: 
   http://stackoverflow.com/questions/3540269/noclassdeffounderror-when-running-shell-script)
*/

@interface PtyView (PtyPrivate)
-(int)count: (NSString *) str;
...
@end

@implementation PtyView (PtyPrivate)

-(int)count: (NSString *) str {
   static int counter = 0;
   if (   
     ([str rangeOfString:@"Charlie>"].location != NSNotFound ) || \
     ([str rangeOfString:@"[Charlie] user>"].location != NSNotFound )
   )
  {
     counter++;
  }
  return counter;
}

-(void)startTask
{
   NSString * cmd = @"/path/to/charliebot/server.sh";
   //NSString * cmd = @"/bin/sh";
   ...
   [self insertText: @"\n\n"];
}

-(void) didRead: (NSNotification *)noty
{
   NSData * data = [[noty userInfo] objectForKey:NSFileHandleNotificationDataItem];

   if ([data length] == 0)
      return; // end of file

   NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

   int printvar = [self count: str];

   if   (printvar < 1 )
   {
      [self insertText: @"."];
      [str release];
      [[noty object] readInBackgroundAndNotify];
   }else if (printvar == 1) {
      [self insertText: @"\n\n"];
      [self insertText: str];
      [str release];
      [[noty object] readInBackgroundAndNotify];
   }else {
      [self insertText: str];
      [str release];
      [[noty object] readInBackgroundAndNotify];
   }
}

@end