phpwebsocketptyreactphp

Resize pseudo tty allocated by php


I am trying to develop a web console using php and xterm.js,

I managed to get the pseudo tty allocated and attach it to xterm.js via websocket but I am not able to tell the process what is the size of terminal to make it work correctly with the size, and I couldn't find any documentation for this.

// using react/child-process
$process = new Process('/usr/bin/env bash -l', null, null, [
    0 => ['pty', 'r'],
    1 => ['pty', 'w'],
    2 => ['pty', 'w'],
]);
$process->start($this->loop);

Solution

  • I found out run stty on the allocated devpts will do the tricks

    if (isset($this->processes[spl_object_id($conn)])) {
        $process = $this->processes[spl_object_id($conn)];
        $data = unpack('i3', $raw);
        $col = $data[2];
        $row = $data[3];
        $getstream = (function () {
            return $this->stream;
        });
        $stty = new Process("stty cols $col rows $row", null, null, [
            $getstream->call($process->stdin), // ex: /dev/pts/0
            $getstream->call($process->stdout),
            $getstream->call($process->stderr),
        ]);
        $stty->start($this->loop);
    }
    

    alternatively can do this

    use React\ChildProcess\Process;
    $gettty = escapeshellcmd(PHP_BINARY).' -r "echo posix_ttyname(STDIN).PHP_EOL;"';
    $bash = "setsid bash -l"; // setsid is important here
    $process = new Process(
        "$gettty && $bash",
        null, null,
        [
            ['pty', 'r'],
            ['pty', 'w'],
            ['pty', 'w'],
        ]
    );
    // TODO: read the first line and get the tty path so can run stty on it later.
    

    Here is a simple example: https://gist.github.com/eslym/d3bd7809681aa9c1eb34913043df9bb6