From 74947d01850c1b96c017e14689e9ca77359ff052 Mon Sep 17 00:00:00 2001 From: Keita Haga Date: Tue, 2 Aug 2011 16:59:23 +0900 Subject: [PATCH 1/2] examples/keyval2.nim: the indices of the elements of a sequence are numbered starting with zero --- examples/keyval.nim | 2 +- examples/keyval2.nim | 4 ++-- examples/myfile.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/keyval.nim b/examples/keyval.nim index 19a430a1ee..ae8cb8f08a 100755 --- a/examples/keyval.nim +++ b/examples/keyval.nim @@ -3,7 +3,7 @@ import re for x in lines("myfile.txt"): if x =~ re"(\w+)=(.*)": - echo "Key: ", matches[0], + echo "Key: ", matches[0], " Value: ", matches[1] diff --git a/examples/keyval2.nim b/examples/keyval2.nim index 5e7d0ea4a2..2a56432768 100755 --- a/examples/keyval2.nim +++ b/examples/keyval2.nim @@ -3,5 +3,5 @@ import pegs for x in lines("myfile.txt"): if x =~ peg"{\ident} \s* '=' \s* {.*}": - echo "Key: ", matches[1], - " Value: ", matches[2] + echo "Key: ", matches[0], + " Value: ", matches[1] diff --git a/examples/myfile.txt b/examples/myfile.txt index 1df0d56f86..fb7cda984e 100755 --- a/examples/myfile.txt +++ b/examples/myfile.txt @@ -6,6 +6,6 @@ asdflksadlfasf adsfljksadfl -key=/usr/bin/value +key=/usr/bin/value key2=/ha/ha From 3e660efeb46a0c73bd3372c4006bf89050b94774 Mon Sep 17 00:00:00 2001 From: dom96 Date: Wed, 3 Aug 2011 22:38:21 +0100 Subject: [PATCH 2/2] Fixed osproc.terminate for posix and improved some other osproc posix functions. --- lib/pure/osproc.nim | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 0beaf710f2..bf5dc79707 100755 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -496,6 +496,9 @@ elif not defined(useNimRtl): else: if dup2(p_stderr[writeIdx], 2) < 0: OSError() + # Create a new process group + if setpgid(0, 0) == -1: quit("setpgid call failed: " & $strerror(errno)) + if workingDir.len > 0: os.setCurrentDir(workingDir) if poUseShell notin options: var a = toCStringArray([extractFilename(command)], args) @@ -534,33 +537,39 @@ elif not defined(useNimRtl): discard close(p.errorHandle) proc suspend(p: PProcess) = - discard kill(p.id, SIGSTOP) + if kill(-p.id, SIGSTOP) != 0'i32: OSError() proc resume(p: PProcess) = - discard kill(p.id, SIGCONT) + if kill(-p.id, SIGCONT) != 0'i32: OSError() proc running(p: PProcess): bool = - result = waitPid(p.id, p.exitCode, WNOHANG) == int(p.id) + var ret = waitPid(p.id, p.exitCode, WNOHANG) + if ret == 0: return true # Can't establish status. Assume running. + result = ret == int(p.id) proc terminate(p: PProcess) = - if kill(p.id, SIGTERM) == 0'i32: - if running(p): discard kill(p.id, SIGKILL) + if kill(-p.id, SIGTERM) == 0'i32: + if p.running(): + if kill(-p.id, SIGKILL) != 0'i32: OSError() + else: OSError() proc waitForExit(p: PProcess): int = #if waitPid(p.id, p.exitCode, 0) == int(p.id): # ``waitPid`` fails if the process is not running anymore. But then # ``running`` probably set ``p.exitCode`` for us. Since ``p.exitCode`` is # initialized with -3, wrong success exit codes are prevented. - var oldExitCode = p.exitCode + if p.exitCode != -3: return p.exitCode if waitPid(p.id, p.exitCode, 0) < 0: - # failed, so restore old exitCode - p.exitCode = oldExitCode + p.exitCode = -3 + OSError() result = int(p.exitCode) proc peekExitCode(p: PProcess): int = - var b = waitPid(p.id, p.exitCode, WNOHANG) == int(p.id) + if p.exitCode != -3: return p.exitCode + var ret = waitPid(p.id, p.exitCode, WNOHANG) + var b = ret == int(p.id) if b: result = -1 - elif p.exitCode == -3: result = -1 + if p.exitCode == -3: result = -1 else: result = p.exitCode proc inputStream(p: PProcess): PStream =