perhaps like this
    
    
    #include <stdio.h>
    
    int main() {
        fprintf(stdout, "This is from hello to stdout\n");
        fprintf(stderr, "This is from hello to stderr\n");
        return 0;
    }
    
    
    Run

and our nim file
    
    
    import os, osproc, selectors
    when defined(windows):
      import streams
    
    proc main =
      when defined(linux):
        let prc = startProcess("./a.out", options = {})
        let errh = errorHandle prc
        let outh = outputHandle prc
        var stor = newSelector[string]()
        defer: stor.close()
        stor.registerHandle(int errh, {Read}, "")
        stor.registerHandle(int outh, {Read}, "")
        var tempout, temperr: File
        running = tempout.open(outh)
        running = temperr.open(errh)
        var running = true
        while running:
          let keys = stor.select -1
          for k in keys:
            if k.errorCode.int != 0:
              echo k.errorCode.osErrorMsg()
              running = false
              continue
            if k.fd == outh:
              echo "stdout: ", tempout.readLine
            elif k.fd == errh:
              echo "stderr: ", temperr.readLine
        close prc
      else:
        echo "on windows"
        let prc = startProcess("./a.exe", options = {})
        var errstream = errorStream prc
        var outstream = outputStream prc
        while true:
          var data = errstream.readLine
          if data == "":
            break
          else:
            echo "stderr: ", data
          data = outstream.readLine
          if data == "":
            break
          else:
            echo "stdout: ", data
    
    main()
    
    
    Run

Note that on windows, selectors doesn't work with file, trying to use 
createIoCompletionPort apparently couldn't work because the pipe is busy, 
because of this also we cannot use ReOpenFile to enable flag 
FILE_FLAG_OVERLAPPED. But, we got stream there so we can emulate async with 
peek proc variants (peekStr and peekLine). cmiiw

Reply via email to