summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2013-01-27 23:58:38 +0000
committerDominik Picheta <dominikpicheta@googlemail.com>2013-01-27 23:58:38 +0000
commit0087828e126679b3e4cabecc81f0a04cafb1a4f2 (patch)
tree243ac042c7f0b37de60715458cd15e4a680b36b5 /lib
parent6fc49914155ad7e96011c705e1749b4bb851165c (diff)
downloadNim-0087828e126679b3e4cabecc81f0a04cafb1a4f2.tar.gz
Fixes #317
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/osproc.nim41
1 files changed, 29 insertions, 12 deletions
diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim
index ecf38f9f6..89914feeb 100755
--- a/lib/pure/osproc.nim
+++ b/lib/pure/osproc.nim
@@ -28,6 +28,7 @@ type
       id: THandle
     else:
       inputHandle, outputHandle, errorHandle: TFileHandle
+      inStream, outStream, errStream: PStream
       id: TPid
     exitCode: cint
 
@@ -112,13 +113,22 @@ proc peekExitCode*(p: PProcess): int {.tags: [].}
   ## return -1 if the process is still running. Otherwise the process' exit code
 
 proc inputStream*(p: PProcess): PStream {.rtl, extern: "nosp$1", tags: [].}
-  ## opens ``p``'s input stream for writing to
+  ## returns ``p``'s input stream for writing to
+  ##
+  ## **Warning**: The returned `PStream` should not be closed manually as it 
+  ## is closed when closing the PProcess ``p``.
 
 proc outputStream*(p: PProcess): PStream {.rtl, extern: "nosp$1", tags: [].}
-  ## opens ``p``'s output stream for reading from
+  ## returns ``p``'s output stream for reading from
+  ##
+  ## **Warning**: The returned `PStream` should not be closed manually as it 
+  ## is closed when closing the PProcess ``p``.
 
 proc errorStream*(p: PProcess): PStream {.rtl, extern: "nosp$1", tags: [].}
-  ## opens ``p``'s output stream for reading from
+  ## returns ``p``'s output stream for reading from
+  ##
+  ## **Warning**: The returned `PStream` should not be closed manually as it 
+  ## is closed when closing the PProcess ``p``.
 
 when defined(macosx) or defined(bsd):
   const
@@ -637,7 +647,20 @@ elif not defined(useNimRtl):
       discard close(p_stdin[readIdx])
       discard close(p_stdout[writeIdx])
 
+    proc createStream(stream: var PStream, handle: var TFileHandle,
+                      fileMode: TFileMode) =
+      var f: TFile
+      if not open(f, handle, fileMode): OSError()
+      stream = newFileStream(f)
+
+    createStream(result.inStream, result.inputHandle, fmWrite)
+    createStream(result.outStream, result.outputHandle, fmRead)
+    createStream(result.errStream, result.errorHandle, fmRead)
+
   proc close(p: PProcess) =
+    close(p.inStream)
+    close(p.outStream)
+    close(p.errStream)
     discard close(p.inputHandle)
     discard close(p.outputHandle)
     discard close(p.errorHandle)
@@ -679,19 +702,13 @@ elif not defined(useNimRtl):
     else: result = p.exitCode.int shr 8
 
   proc inputStream(p: PProcess): PStream =
-    var f: TFile
-    if not open(f, p.inputHandle, fmWrite): OSError()
-    result = newFileStream(f)
+    return p.inStream
 
   proc outputStream(p: PProcess): PStream =
-    var f: TFile
-    if not open(f, p.outputHandle, fmRead): OSError()
-    result = newFileStream(f)
+    return p.outStream
 
   proc errorStream(p: PProcess): PStream =
-    var f: TFile
-    if not open(f, p.errorHandle, fmRead): OSError()
-    result = newFileStream(f)
+    return p.errStream
 
   proc csystem(cmd: cstring): cint {.nodecl, importc: "system".}