Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2011-08-04 19:47:47 +02:00
commit 7748dbc0b2
4 changed files with 23 additions and 14 deletions

View file

@ -3,5 +3,5 @@ import pegs
for x in lines("myfile.txt"): for x in lines("myfile.txt"):
if x =~ peg"{\ident} \s* '=' \s* {.*}": if x =~ peg"{\ident} \s* '=' \s* {.*}":
echo "Key: ", matches[1], echo "Key: ", matches[0],
" Value: ", matches[2] " Value: ", matches[1]

View file

@ -496,6 +496,9 @@ elif not defined(useNimRtl):
else: else:
if dup2(p_stderr[writeIdx], 2) < 0: OSError() 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 workingDir.len > 0: os.setCurrentDir(workingDir)
if poUseShell notin options: if poUseShell notin options:
var a = toCStringArray([extractFilename(command)], args) var a = toCStringArray([extractFilename(command)], args)
@ -534,33 +537,39 @@ elif not defined(useNimRtl):
discard close(p.errorHandle) discard close(p.errorHandle)
proc suspend(p: PProcess) = proc suspend(p: PProcess) =
discard kill(p.id, SIGSTOP) if kill(-p.id, SIGSTOP) != 0'i32: OSError()
proc resume(p: PProcess) = proc resume(p: PProcess) =
discard kill(p.id, SIGCONT) if kill(-p.id, SIGCONT) != 0'i32: OSError()
proc running(p: PProcess): bool = 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) = proc terminate(p: PProcess) =
if kill(p.id, SIGTERM) == 0'i32: if kill(-p.id, SIGTERM) == 0'i32:
if running(p): discard kill(p.id, SIGKILL) if p.running():
if kill(-p.id, SIGKILL) != 0'i32: OSError()
else: OSError()
proc waitForExit(p: PProcess): int = proc waitForExit(p: PProcess): int =
#if waitPid(p.id, p.exitCode, 0) == int(p.id): #if waitPid(p.id, p.exitCode, 0) == int(p.id):
# ``waitPid`` fails if the process is not running anymore. But then # ``waitPid`` fails if the process is not running anymore. But then
# ``running`` probably set ``p.exitCode`` for us. Since ``p.exitCode`` is # ``running`` probably set ``p.exitCode`` for us. Since ``p.exitCode`` is
# initialized with -3, wrong success exit codes are prevented. # 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: if waitPid(p.id, p.exitCode, 0) < 0:
# failed, so restore old exitCode p.exitCode = -3
p.exitCode = oldExitCode OSError()
result = int(p.exitCode) result = int(p.exitCode)
proc peekExitCode(p: PProcess): int = 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 if b: result = -1
elif p.exitCode == -3: result = -1 if p.exitCode == -3: result = -1
else: result = p.exitCode else: result = p.exitCode
proc inputStream(p: PProcess): PStream = proc inputStream(p: PProcess): PStream =