title: Node.js spawning a child process interactively with separate stdout and stderr streams
body: <p>Consider the following C program (test.c):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() {
    printf("string out 1\n");
    fprintf(stderr, "string err 1\n");
    getchar();
    printf("string out 2\n");
    fprintf(stderr, "string err 2\n");
    fclose(stdout);
  }
  </code></pre>
  
  <p>Which should print a line to stdout, a line to stderr, then wait for user input, then another line to stdout and another line to stderr. Very basic!
  When compiled and run on the command line the output of the program when complete (user input is received for getchar()):</p>
  
  <pre><code>$ ./test 
  string out 1
  string err 1
  
  string out 2
  string err 2
  </code></pre>
  
  <p>When trying to spawn this program as a child process using nodejs with the following code:</p>
  
  <pre><code>var TEST_EXEC = './test';
  
  var spawn = require('child_process').spawn;
  var test = spawn(TEST_EXEC);
  
  test.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
  });
  
  test.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
  });
  
  // Simulate entering data for getchar() after 1 second
  setTimeout(function() {
    test.stdin.write('\n');
  }, 1000);
  </code></pre>
  
  <p>The output appears like this:</p>
  
  <pre><code>$ nodejs test.js 
  stderr: string err 1
  
  stdout: string out 1
  string out 2
  
  stderr: string err 2
  </code></pre>
  
  <p>Very different from the output as seen when running ./test in the terminal. This is because the ./test program isn't running in an interactive shell when spawned by nodejs. The test.c stdout stream is buffered and when run in a terminal as soon as a \n is reached the buffer is flushed but when spawned in this way with node the buffer isn't flushed. This could be resolved by either flushing stdout after every print, or changing the stdout stream to be unbuffered so it flushes everything immediately.
  Assuming that test.c source isn't available or modifiable, neither of the two flushing options mentioned can be implemented.</p>
  
  <p>I then started looking at emulating an interactive shell, there's pty.js (pseudo terminal) which does a good job, for example:</p>
  
  <pre><code>var spawn = require('pty.js').spawn;
  var test = spawn(TEST_EXEC);
  
  test.on('data', function (data) {
    console.log('data: ' + data);
  });
  
  // Simulate entering data for getchar() after 1 second
  setTimeout(function() {
    test.write('\n');
  }, 1000);
  </code></pre>
  
  <p>Which outputs:</p>
  
  <pre><code>$ nodejs test.js
  data: string out 1
  string err 1
  
  data: 
  
  data: string out 2
  string err 2
  </code></pre>
  
  <p>However both stdout and stderr are merged together (as you would see when running the program in a terminal) and I can't think of a way to separate the data from the streams.</p>
  
  <p>So the question..</p>
  
  <p>Is there any way using nodejs to achieve the output as seen when running ./test without modifying the test.c code? Either by terminal emulation or process spawning or any other method?</p>
  
  <p>Cheers!</p>