Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560430
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:55:46+00:00 2026-05-21T17:55:46+00:00

I’m aware of libexpect , but its source is huge and requires tcl. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:55:46+00:00Added an answer on May 21, 2026 at 5:55 pm

    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 -> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.