=== modified file 'Makefile' --- Makefile 2019-04-04 19:48:08 +0000 +++ Makefile 2019-04-01 12:39:56 +0000 @@ -1,3 +1,2 @@ -#--embedsrc ppio: ppio.nim - nim c --debugger:native ppio.nim + nim c ppio.nim === modified file 'ppio.nim' --- ppio.nim 2019-04-04 19:48:08 +0000 +++ ppio.nim 2019-04-04 17:35:39 +0000 @@ -3,14 +3,12 @@ import posix import asyncfutures import asyncdispatch -import strformat const ERR_PERM = 100 ERR_TEMP = 101 ERR_EXEC_OTHER = 126 ERR_EXEC_ENOENT = 127 - DEBUG_OUT {.intdefine.}: int = 0 proc writeHelp(errorcode: int): void = write(stdout, "writeHelp()\n") @@ -96,49 +94,32 @@ if flags == -1: libc_fatal("fcntl():") if posix.fcntl(fd_from, posix.F_SETFL, flags or posix.O_NONBLOCK) == -1: libc_fatal("fcntl():") - asyncdispatch.register(fd_from.AsyncFD) flags = posix.fcntl(fd_to, posix.F_GETFL) if flags == -1: libc_fatal("fcntl():") if posix.fcntl(fd_to, posix.F_SETFL, flags or posix.O_NONBLOCK) == -1: libc_fatal("fcntl():") - asyncdispatch.register(fd_to.AsyncFD) var future_result = newFuture[void]("copy_data") buf_size = 4096 # we could query posix.PC_PIPE_BUF, but this is common buffer = newString(buf_size) buf_written = 0 - reading = true - writing = false - when DEBUG_OUT.bool: - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" # Mutually referential function declaration proc read_cb(fd: AsyncFD): bool {.gcsafe.} proc write_cb(fd: AsyncFD): bool {.gcsafe.} proc read_cb(fd: AsyncFD): bool {.gcsafe.} = - when DEBUG_OUT.bool: - echo fmt"read_cb(" & $fd.int & ")" - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" - assert(reading) - assert(not writing) let res = posix.read(fd_from, addr buffer[0], buf_size) if res < 0: let lastError = osLastError() if lastError.int32 in {EINTR, EWOULDBLOCK, EAGAIN}: - when DEBUG_OUT.bool: - echo "return false" - return false # try again + return true # try again else: future_result.fail(newException(OSError, osErrorMsg(lastError))) elif res == 0: # End of "file" - asyncdispatch.unregister(fd_from.AsyncFD) - asyncdispatch.unregister(fd_to.AsyncFD) - when DEBUG_OUT.bool: - echo "close(", fd_to, ")" if posix.close(fd_to) != 0: let lastError = osLastError() future_result.fail(newException(OSError, osErrorMsg(lastError))) @@ -146,58 +127,30 @@ future_result.complete() else: buffer.setLen(res) - when DEBUG_OUT.bool: - echo "read[" & $fd.int & "] -> " & repr(buffer) - writing = true - when DEBUG_OUT.bool: - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" asyncdispatch.addWrite(fd_to.AsyncFD, write_cb) - reading = false - when DEBUG_OUT.bool: - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" - echo "return true" - return true + return false proc write_cb(fd: AsyncFD): bool {.gcsafe.} = - when DEBUG_OUT.bool: - echo fmt"write_cb(" & $fd.int & ")" - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" - assert(writing) - assert(not reading) let remaining = buffer.len - buf_written - when DEBUG_OUT.bool: - echo fmt"written: {buf_written} remaining: {remaining}" - #var d = cast[cstring](buffer) - #let res = posix.write(fd_to, addr d[buf_written], remaining.cint) - let res = posix.write(fd_to, addr buffer[buf_written], remaining.cint) + var d = cast[cstring](buffer) + let res = posix.write(fd_to, addr d[buf_written], remaining.cint) if res < 0: let lastError = osLastError() if lastError.int32 in {EINTR, EWOULDBLOCK, EAGAIN}: - when DEBUG_OUT.bool: - echo "return false" - return false # try again + return true # try again else: future_result.fail(newException(OSError, osErrorMsg(lastError))) - writing = false - when DEBUG_OUT.bool: - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" - echo "return true" - return true + return false else: buf_written.inc(res) if res < remaining: - return false # more to write + return true # more to write else: - reading = true asyncdispatch.addRead(fd_from.AsyncFD, read_cb) buf_written = 0 buffer.setLen(buf_size) - writing = false - when DEBUG_OUT.bool: - echo fmt"reading[{fd_from}]: {reading} writing[{fd_to}]: {writing}" - echo "return true" - return true + return false asyncdispatch.addRead(fd_from.AsyncFD, read_cb) return future_result @@ -243,10 +196,9 @@ quit(ERR_TEMP) else: # Parent process - when DEBUG_OUT.bool: - echo "pipein: " & repr(pipein) - echo "pipeout: " & repr(pipeout) + echo "pipein: " & repr(pipein) if close(pipein[0]) != 0: libc_fatal("close():") + echo "pipeout: " & repr(pipein) if close(pipeout[1]) != 0: libc_fatal("close():") # input: 0 -> pipein[1]