Method: PTY.spawn

Defined in:
pty.c

.spawn(command_line) {|r, w, pid| ... } ⇒ Object .spawn(command_line) ⇒ Array .spawn(command, arguments, ...) {|r, w, pid| ... } ⇒ Object .spawn(command, arguments, ...) ⇒ Array

Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty.

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

command and command_line are the full commands to run, given a String. Any additional arguments will be passed to the command.

Return values

In the non-block form this returns an array of size three, [r, w, pid].

In the block form these same values will be yielded to the block:

r

A readable IO that contains the command’s standard output and standard error

w

A writable IO that is the command’s standard input

pid

The process identifier for the command.

Overloads:

  • .spawn(command_line) {|r, w, pid| ... } ⇒ Object

    Yields:

    • (r, w, pid)
  • .spawn(command_line) ⇒ Array

    Returns:

    • (Array)
  • .spawn(command, arguments, ...) {|r, w, pid| ... } ⇒ Object

    Yields:

    • (r, w, pid)
  • .spawn(command, arguments, ...) ⇒ Array

    Returns:

    • (Array)


575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'pty.c', line 575

static VALUE
pty_getpty(int argc, VALUE *argv, VALUE self)
{
    VALUE res;
    struct pty_info info;
    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_modestr_fmode("r");
    rfptr->fd = info.fd;
    rfptr->pathv = rb_obj_freeze(rb_str_new_cstr(SlaveName));

    wfptr->mode = rb_io_modestr_fmode("w") | FMODE_SYNC;
    wfptr->fd = rb_cloexec_dup(info.fd);
    if (wfptr->fd == -1)
        rb_sys_fail("dup()");
    rb_update_max_fd(wfptr->fd);
    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));

    if (rb_block_given_p()) {
	rb_ensure(rb_yield, res, pty_detach_process, (VALUE)&info);
	return Qnil;
    }
    return res;
}