• Question about writing and reading from a PTY

    From Chad Woolley@21:1/5 to All on Sat Jan 14 11:32:56 2017
    Hello,

    I'm trying to understand PTY behavior (I'm using Ruby 2.3.3).

    Specifically, in line 86 of this example (which should be executable if downloaded), why does the command not behave the same way via PTY as it does from a real terminal (which is to echo '>a<' or '>z<' to STDOUT)?

    https://gist.github.com/thewoolleyman/6a060574f22eafd42955812a1a2a7842/37491a2c756aa066444626294d65ab9208c95c7c#file-pty_check_test-rb-L86


    Specifically, this is expected (which is what happens if you run the cmd from the command line:

    ```
    $ ruby -e 'require "io/console"; while(i=$stdin.getch) do puts ">#{i}<"; if i =~ /z/; break; end; end'
    a<
    z<
    $ # back at prompt
    ```

    But this program instead does this:

    ```
    cmd: ruby -e 'require "io/console"; while(i=$stdin.getch) do puts ">#{i}<"; if i =~ /z/; break; end; end'
    Process with pid 24846 is running
    PTY::check for process with pid 24846 is : nil
    writing "a"
    reading...
    output_buffer contents:
    a<
    Process with pid 24846 is running
    PTY::check for process with pid 24846 is : nil
    writing "z"
    reading...
    output_buffer contents:
    a<

    z
    Process with pid 24846 is running
    PTY::check for process with pid 24846 is : nil
    a<

    z

    (program exits)
    ```

    I'm aware that a real program would have no sleeps, and would have more sophisticated handling of input/output stream blocking, interrupts, exceptions, etc, but I'm trying to make this example as simple as possible.

    I suspect that the program is not processing the last "z" input character, and thus not exiting, but I I'm not sure how to prove that, nor do I understand why it behaves differently in a real terminal.

    Thanks!
    -- Chad

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chad Woolley@21:1/5 to Chad Woolley on Sun Jan 15 20:06:23 2017
    On Saturday, January 14, 2017 at 12:33:04 PM UTC-7, Chad Woolley wrote:
    Hello,

    I'm trying to understand PTY behavior (I'm using Ruby 2.3.3).

    Specifically, in line 86 of this example (which should be executable if downloaded), why does the command not behave the same way via PTY as it does from a real terminal (which is to echo '>a<' or '>z<' to STDOUT)?

    https://gist.github.com/thewoolleyman/6a060574f22eafd42955812a1a2a7842/37491a2c756aa066444626294d65ab9208c95c7c#file-pty_check_test-rb-L86


    Figured this out myself. I wasn't reading the CR+LF chars appended by the 'puts' in the cmd. Fixed by adding a loop to ensure I read all chars (with the help of IO::ready? in 'io/wait' module).

    Here's the working version and output:

    https://gist.github.com/thewoolleyman/6a060574f22eafd42955812a1a2a7842/c76e0c62253c85033e73892319404eb9a40ab3e6#file-pty_check_test-rb-L42

    https://gist.github.com/thewoolleyman/6a060574f22eafd42955812a1a2a7842/c76e0c62253c85033e73892319404eb9a40ab3e6#file-output-txt

    -- Chad

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)