In Files

  • pty/pty.c

Class/Module Index [+]

Quicksearch

PTY

Public Class Methods

spawn(command...) {|r, w, pid| ... } => nil click to toggle source
spawn(command...) => r, w, pid
getpty(command...) {|r, w, pid| ... } => nil
getpty(command...) => r, w, pid

spawns the specified command on a newly allocated pty.

The command's controlling tty is set to the slave device of the pty. Also its standard input/output/error is redirected to the slave device.

::spawn returns two IO objects and PID. PID is the process ID of the command. The two IO objects are connected to the master device of the pty. The first IO object is opened as read mode and The second is opened as write mode.

If a block is given, two IO objects and PID is yielded.

 
               static VALUE
pty_getpty(int argc, VALUE *argv, VALUE self)
{
    VALUE res;
    struct pty_info info;
    struct pty_info thinfo;
    rb_io_t *wfptr,*rfptr;
    VALUE rport = rb_obj_alloc(rb_cFile);
    VALUE wport = rb_obj_alloc(rb_cFile);
    char SlaveName[DEVICELEN];

    MakeOpenFile(rport, rfptr);
    MakeOpenFile(wport, wfptr);

    establishShell(argc, argv, &info, SlaveName);

    rfptr->mode = rb_io_mode_flags("r");
    rfptr->fd = info.fd;
    rfptr->pathv = rb_obj_freeze(rb_str_new_cstr(SlaveName));

    wfptr->mode = rb_io_mode_flags("w") | FMODE_SYNC;
    wfptr->fd = dup(info.fd);
    if (wfptr->fd == -1)
        rb_sys_fail("dup()");
    wfptr->pathv = rfptr->pathv;

    res = rb_ary_new2(3);
    rb_ary_store(res,0,(VALUE)rport);
    rb_ary_store(res,1,(VALUE)wport);
    rb_ary_store(res,2,PIDT2NUM(info.child_pid));

    thinfo.thread = rb_thread_create(pty_syswait, (void*)&info);
    thinfo.child_pid = info.child_pid;
    rb_thread_schedule();

    if (rb_block_given_p()) {
        rb_ensure(rb_yield, res, pty_finalize_syswait, (VALUE)&thinfo);
        return Qnil;
    }
    return res;
}
            
protect_signal() click to toggle source

ruby function: ::protect_signal - obsolete

 
               static VALUE
pty_protect(VALUE self)
{
    rb_warn("PTY::protect_signal is no longer needed");
    rb_yield(Qnil);
    return self;
}
            
reset_signal() click to toggle source

ruby function: ::reset_signal - obsolete

 
               static VALUE
pty_reset_signal(VALUE self)
{
    rb_warn("PTY::reset_signal is no longer needed");
    return self;
}
            
spawn(command...) {|r, w, pid| ... } => nil click to toggle source
spawn(command...) => r, w, pid
getpty(command...) {|r, w, pid| ... } => nil
getpty(command...) => r, w, pid

spawns the specified command on a newly allocated pty.

The command's controlling tty is set to the slave device of the pty. Also its standard input/output/error is redirected to the slave device.

::spawn returns two IO objects and PID. PID is the process ID of the command. The two IO objects are connected to the master device of the pty. The first IO object is opened as read mode and The second is opened as write mode.

If a block is given, two IO objects and PID is yielded.

 
               static VALUE
pty_getpty(int argc, VALUE *argv, VALUE self)
{
    VALUE res;
    struct pty_info info;
    struct pty_info thinfo;
    rb_io_t *wfptr,*rfptr;
    VALUE rport = rb_obj_alloc(rb_cFile);
    VALUE wport = rb_obj_alloc(rb_cFile);
    char SlaveName[DEVICELEN];

    MakeOpenFile(rport, rfptr);
    MakeOpenFile(wport, wfptr);

    establishShell(argc, argv, &info, SlaveName);

    rfptr->mode = rb_io_mode_flags("r");
    rfptr->fd = info.fd;
    rfptr->pathv = rb_obj_freeze(rb_str_new_cstr(SlaveName));

    wfptr->mode = rb_io_mode_flags("w") | FMODE_SYNC;
    wfptr->fd = dup(info.fd);
    if (wfptr->fd == -1)
        rb_sys_fail("dup()");
    wfptr->pathv = rfptr->pathv;

    res = rb_ary_new2(3);
    rb_ary_store(res,0,(VALUE)rport);
    rb_ary_store(res,1,(VALUE)wport);
    rb_ary_store(res,2,PIDT2NUM(info.child_pid));

    thinfo.thread = rb_thread_create(pty_syswait, (void*)&info);
    thinfo.child_pid = info.child_pid;
    rb_thread_schedule();

    if (rb_block_given_p()) {
        rb_ensure(rb_yield, res, pty_finalize_syswait, (VALUE)&thinfo);
        return Qnil;
    }
    return res;
}