PseudoTTY以编程方式执行命令

时间:2012-01-05 21:13:02

标签: objective-c cocoa pty

有一个名为“PseudoTTY”的开源Xcode项目用objective-c编写,我试图找出如何以编程方式执行命令。当我编译应用程序时,我得到一个很好的小终端窗口,它完全符合我的要求;除了我必须手动输入命令。

我想要做的是在终端中以编程方式执行命令,并能够使用我的程序解析结果。

感兴趣的景点是:

- (void)keyDown:(NSEvent *)event
{
    const char * typein = [[event characters] UTF8String];

    [[pty_ masterFileHandle]
          writeData:[NSData dataWithBytes:typein length:strlen(typein)]];
}

-(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];

    [self insertText:str];

1 个答案:

答案 0 :(得分:0)

您需要做的就是用您选择的字符串替换typein,它会做您想要的。输出将在str方法的didRead:中。

char *myCommand = "ls";
[[pty_ masterFileHandle] writeData:[NSData dataWithBytes:myCommand 
                                                  length:strlen(myCommand)]];