有没有像 ruby​​ 的“expect.rb”之类的东西?但对于 Objective C 呢?

发布于 2024-11-03 04:50:11 字数 219 浏览 1 评论 0原文

我知道 libexpect,但它的源代码很大并且需要 tcl。我希望有像 ruby​​ 的 'expect.rb' 这样小的东西一个小文件。有什么想法吗?

I'm aware of libexpect, but its source is huge and requires tcl. I was hoping for something just as small as ruby's 'expect.rb' which is a tiny file. Any ideas?

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

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

发布评论

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

评论(1

╭⌒浅淡时光〆 2024-11-10 04:50:11

解决了。我对 libexpect 一点运气都没有。相反,我只是使用 < 将 ruby​​s 'expect.rb' 移植到 Objective-c a href="http://limechat.net/cocoaoniguruma/" rel="nofollow">CocoaOniguruma。请随意使用它。


/*
NSFileHandle+Expect.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>


@class ExpectResult;

@interface NSFileHandle (Expect)

/*
wait for activity on the file descriptor.
stops waiting if it takes longer than X seconds.
*/
-(BOOL)waitForData:(float)seconds;


/*
buffer data on the filedescriptor until it matches the specified pattern.
*/
-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug;


/*
write to filedescriptor
*/
-(void)writeAsciiString:(NSString*)s;

@end

/*
NSFileHandle+Expect.m
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "NSFileHandle+Expect.h"
#import "OnigRegexp.h"
#import "ExpectResult.h"


@implementation NSFileHandle (Expect)

-(BOOL)waitForData:(float)seconds {
    struct timeval t; 
    t.tv_sec = (int)seconds;
    float remain = seconds - t.tv_sec;
    t.tv_usec = (int)(remain * 1000000);


    int fd = [self fileDescriptor];
    fd_set ready; 
    FD_ZERO(&ready); 
    FD_SET((unsigned int)fd, &ready); 

    int res = select(fd+1, &ready, NULL, NULL, &t); 
    if(res == 0) {
        return NO; // timeout
    }
    if(FD_ISSET(fd, &ready)) {
        return YES; // we have data, one or more bytes is ready
    }
    return NO; // error
}


-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug {
    OnigRegexp* regexp = [OnigRegexp compile:pattern];
    NSMutableString* buffer = [NSMutableString stringWithCapacity:100];
    ExpectResult* result = nil;
    while(1) {
        // wait until 1 byte is ready
        if(![self waitForData:seconds]) {
            // timeout or error
            result = nil;
            break;
        }

        // read out the byte and append it to the buffer
        NSData* char_data = [self readDataOfLength:1];
        NSString* char_string = [[NSString alloc] initWithData:char_data encoding: NSASCIIStringEncoding];
        [buffer appendString:char_string];
        if(debug) {
            NSLog(@"%s %@", _cmd, char_string);
        }
        [char_string release];

        // see if the new buffer now satisfies the pattern
        OnigResult* r = [regexp search:buffer];
        if(r) {
            result = [[[ExpectResult alloc] init] autorelease];
            result.bufferString = [NSString stringWithString:buffer];
            result.onigResult = r;
            break;
        }
    }

    return result;
}

-(void)writeAsciiString:(NSString*)s {
    [self writeData:[s dataUsingEncoding:NSASCIIStringEncoding]];   
}

@end

/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>

@class OnigResult;

@interface ExpectResult : NSObject {
    NSString* m_buffer_string;
    OnigResult* m_onig_result;
}
@property (nonatomic, retain) NSString* bufferString;
@property (nonatomic, retain) OnigResult* onigResult;

@end

/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "ExpectResult.h"
#import "OnigRegexp.h"

@implementation ExpectResult

@synthesize bufferString = m_buffer_string;
@synthesize onigResult = m_onig_result;

-(void)dealloc {
    self.bufferString = nil;
    self.onigResult = nil;
    [super dealloc];
}

@end

NSArray* arguments = [NSArray arrayWithObject:@"ftp.ruby-lang.org"];

NSTask* task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/ftp"];

NSPipe* readPipe = [NSPipe pipe];
NSPipe* writePipe = [NSPipe pipe];

[task setStandardInput: writePipe];
[task setStandardOutput: readPipe];
[task setArguments:arguments];

[task launch];

NSFileHandle* readHandle = [readPipe fileHandleForReading];
NSFileHandle* writeHandle = [writePipe fileHandleForWriting];

{
    NSString* pattern = @"^Name.*: ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"ftp\n"];
}
{
    NSString* pattern = @"word:";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"guest@\n"];
}
{
    NSString* pattern = @"> ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"cd pub/ruby\n"];
}
{
    NSString* pattern = @"> ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"dir\n"];
}
{
    NSString* pattern = @"> ";
    ExpectResult* er = [readHandle expect:pattern timeout:5 debug:YES];

    NSLog(@"%s versions: %@", _cmd, er.bufferString);

    [writeHandle writeAsciiString:@"quit\n"];
}

output:
drwxrwxr-x    2 0        103          4096 Jul 06  2009 1.0
drwxrwxr-x    2 0        103          4096 Aug 04  2003 1.1a
drwxrwxr-x    2 0        103          4096 Jul 16  1998 1.1b
drwxrwxr-x    2 0        103          4096 Jan 18  1999 1.1c
drwxrwxr-x    2 0        103            54 Dec 25  1998 1.1d
drwxrwxr-x    2 0        103          4096 Sep 18  1999 1.2
drwxrwxr-x    2 0        103          4096 Sep 18  1999 1.3
drwxrwxr-x    2 0        103          4096 Apr 05  2001 1.4
drwxrwxr-x    2 0        103          4096 Sep 20  2005 1.6
drwxrwxr-x    2 0        103          8192 Feb 18 12:49 1.8
drwxrwxr-x    2 0        103          4096 Feb 18 13:39 1.9
drwxrwxr-t    6 0        103            89 Jun 15  2004 binaries
drwxrwxr-x    2 1027     100         12288 Apr 05 15:12 doc
lrwxrwxrwx    1 1023     100            27 Sep 23  2010 ruby-1.8.6-p420.tar.bz2 -> 

Solved it. I had no luck with libexpect at all. Instead I have just ported rubys 'expect.rb' to objective-c using CocoaOniguruma. Feel free to use it as you like.


/*
NSFileHandle+Expect.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>


@class ExpectResult;

@interface NSFileHandle (Expect)

/*
wait for activity on the file descriptor.
stops waiting if it takes longer than X seconds.
*/
-(BOOL)waitForData:(float)seconds;


/*
buffer data on the filedescriptor until it matches the specified pattern.
*/
-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug;


/*
write to filedescriptor
*/
-(void)writeAsciiString:(NSString*)s;

@end

/*
NSFileHandle+Expect.m
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "NSFileHandle+Expect.h"
#import "OnigRegexp.h"
#import "ExpectResult.h"


@implementation NSFileHandle (Expect)

-(BOOL)waitForData:(float)seconds {
    struct timeval t; 
    t.tv_sec = (int)seconds;
    float remain = seconds - t.tv_sec;
    t.tv_usec = (int)(remain * 1000000);


    int fd = [self fileDescriptor];
    fd_set ready; 
    FD_ZERO(&ready); 
    FD_SET((unsigned int)fd, &ready); 

    int res = select(fd+1, &ready, NULL, NULL, &t); 
    if(res == 0) {
        return NO; // timeout
    }
    if(FD_ISSET(fd, &ready)) {
        return YES; // we have data, one or more bytes is ready
    }
    return NO; // error
}


-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug {
    OnigRegexp* regexp = [OnigRegexp compile:pattern];
    NSMutableString* buffer = [NSMutableString stringWithCapacity:100];
    ExpectResult* result = nil;
    while(1) {
        // wait until 1 byte is ready
        if(![self waitForData:seconds]) {
            // timeout or error
            result = nil;
            break;
        }

        // read out the byte and append it to the buffer
        NSData* char_data = [self readDataOfLength:1];
        NSString* char_string = [[NSString alloc] initWithData:char_data encoding: NSASCIIStringEncoding];
        [buffer appendString:char_string];
        if(debug) {
            NSLog(@"%s %@", _cmd, char_string);
        }
        [char_string release];

        // see if the new buffer now satisfies the pattern
        OnigResult* r = [regexp search:buffer];
        if(r) {
            result = [[[ExpectResult alloc] init] autorelease];
            result.bufferString = [NSString stringWithString:buffer];
            result.onigResult = r;
            break;
        }
    }

    return result;
}

-(void)writeAsciiString:(NSString*)s {
    [self writeData:[s dataUsingEncoding:NSASCIIStringEncoding]];   
}

@end

/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>

@class OnigResult;

@interface ExpectResult : NSObject {
    NSString* m_buffer_string;
    OnigResult* m_onig_result;
}
@property (nonatomic, retain) NSString* bufferString;
@property (nonatomic, retain) OnigResult* onigResult;

@end

/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license

requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "ExpectResult.h"
#import "OnigRegexp.h"

@implementation ExpectResult

@synthesize bufferString = m_buffer_string;
@synthesize onigResult = m_onig_result;

-(void)dealloc {
    self.bufferString = nil;
    self.onigResult = nil;
    [super dealloc];
}

@end

NSArray* arguments = [NSArray arrayWithObject:@"ftp.ruby-lang.org"];

NSTask* task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/ftp"];

NSPipe* readPipe = [NSPipe pipe];
NSPipe* writePipe = [NSPipe pipe];

[task setStandardInput: writePipe];
[task setStandardOutput: readPipe];
[task setArguments:arguments];

[task launch];

NSFileHandle* readHandle = [readPipe fileHandleForReading];
NSFileHandle* writeHandle = [writePipe fileHandleForWriting];

{
    NSString* pattern = @"^Name.*: ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"ftp\n"];
}
{
    NSString* pattern = @"word:";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"guest@\n"];
}
{
    NSString* pattern = @"> ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"cd pub/ruby\n"];
}
{
    NSString* pattern = @"> ";
    [readHandle expect:pattern timeout:5 debug:YES];
    [writeHandle writeAsciiString:@"dir\n"];
}
{
    NSString* pattern = @"> ";
    ExpectResult* er = [readHandle expect:pattern timeout:5 debug:YES];

    NSLog(@"%s versions: %@", _cmd, er.bufferString);

    [writeHandle writeAsciiString:@"quit\n"];
}

output:
drwxrwxr-x    2 0        103          4096 Jul 06  2009 1.0
drwxrwxr-x    2 0        103          4096 Aug 04  2003 1.1a
drwxrwxr-x    2 0        103          4096 Jul 16  1998 1.1b
drwxrwxr-x    2 0        103          4096 Jan 18  1999 1.1c
drwxrwxr-x    2 0        103            54 Dec 25  1998 1.1d
drwxrwxr-x    2 0        103          4096 Sep 18  1999 1.2
drwxrwxr-x    2 0        103          4096 Sep 18  1999 1.3
drwxrwxr-x    2 0        103          4096 Apr 05  2001 1.4
drwxrwxr-x    2 0        103          4096 Sep 20  2005 1.6
drwxrwxr-x    2 0        103          8192 Feb 18 12:49 1.8
drwxrwxr-x    2 0        103          4096 Feb 18 13:39 1.9
drwxrwxr-t    6 0        103            89 Jun 15  2004 binaries
drwxrwxr-x    2 1027     100         12288 Apr 05 15:12 doc
lrwxrwxrwx    1 1023     100            27 Sep 23  2010 ruby-1.8.6-p420.tar.bz2 -> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文