PseudoTTY 以编程方式执行命令

发布于 2024-12-25 06:46:42 字数 741 浏览 3 评论 0原文

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

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

感兴趣的地方是:

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

There is an open source Xcode project called "PseudoTTY" written in objective-c and I'm trying to find out how to execute commands programmatically. When I compile the application, I get a nice little terminal window that does exactly what I want; except I have to manually type the command in.

What I want to do is programatically execute a command in the terminal, and be able to parse the results with my program.

Spots of interest are:

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

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

and

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2025-01-01 06:46:42

您需要做的就是将 typein 替换为您选择的字符串,它就会执行您想要的操作。输出将位于 didRead: 方法的 str 中。

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

All you need to do is replace typein with a string of your choice and it will do what you want. The output will be in str in the didRead: method.

char *myCommand = "ls";
[[pty_ masterFileHandle] writeData:[NSData dataWithBytes:myCommand 
                                                  length:strlen(myCommand)]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文