mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-29 19:57:58 +00:00
tester checks exitcode; osproc additions; DLL fixes; taint mode fixes
This commit is contained in:
@@ -344,7 +344,7 @@ proc transformFile*(infile, outfile: string,
|
||||
## reads in the file `infile`, performs a parallel replacement (calls
|
||||
## `parallelReplace`) and writes back to `outfile`. Raises ``EIO`` if an
|
||||
## error occurs. This is supposed to be used for quick scripting.
|
||||
var x = readFile(infile)
|
||||
var x = readFile(infile).string
|
||||
writeFile(outfile, x.parallelReplace(subs))
|
||||
|
||||
iterator split*(s: string, sep: TRegEx): string =
|
||||
|
||||
@@ -34,7 +34,7 @@ proc openDefaultBrowser*(url: string) =
|
||||
var u = quoteIfContainsWhite(url)
|
||||
for a in items(attempts):
|
||||
if execShellCmd(a & u) == 0: return
|
||||
for b in getEnv("BROWSER").split(PathSep):
|
||||
for b in getEnv("BROWSER").string.split(PathSep):
|
||||
try:
|
||||
# we use ``startProcess`` here because we don't want to block!
|
||||
discard startProcess(command=b, args=[url], options={poUseShell})
|
||||
|
||||
@@ -74,6 +74,10 @@ when defined(Nimdoc): # only for proper documentation:
|
||||
## The file extension of a script file. For example: "" for POSIX,
|
||||
## "bat" on Windows.
|
||||
|
||||
DynlibFormat* = "lib$1.so"
|
||||
## The format string to turn a filename into a `DLL`:idx: file (also
|
||||
## called `shared object`:idx: on some operating systems).
|
||||
|
||||
elif defined(macos):
|
||||
const
|
||||
curdir* = ':'
|
||||
@@ -84,6 +88,7 @@ elif defined(macos):
|
||||
FileSystemCaseSensitive* = false
|
||||
ExeExt* = ""
|
||||
ScriptExt* = ""
|
||||
DynlibFormat* = "$1.dylib"
|
||||
|
||||
# MacOS paths
|
||||
# ===========
|
||||
@@ -113,6 +118,7 @@ elif doslike:
|
||||
FileSystemCaseSensitive* = false
|
||||
ExeExt* = "exe"
|
||||
ScriptExt* = "bat"
|
||||
DynlibFormat* = "$1.dll"
|
||||
elif defined(PalmOS) or defined(MorphOS):
|
||||
const
|
||||
dirsep* = '/'
|
||||
@@ -122,6 +128,7 @@ elif defined(PalmOS) or defined(MorphOS):
|
||||
FileSystemCaseSensitive* = false
|
||||
ExeExt* = ""
|
||||
ScriptExt* = ""
|
||||
DynlibFormat* = "$1.prc"
|
||||
elif defined(RISCOS):
|
||||
const
|
||||
dirsep* = '.'
|
||||
@@ -131,6 +138,7 @@ elif defined(RISCOS):
|
||||
FileSystemCaseSensitive* = true
|
||||
ExeExt* = ""
|
||||
ScriptExt* = ""
|
||||
DynlibFormat* = "lib$1.so"
|
||||
else: # UNIX-like operating system
|
||||
const
|
||||
curdir* = '.'
|
||||
@@ -141,6 +149,7 @@ else: # UNIX-like operating system
|
||||
FileSystemCaseSensitive* = true
|
||||
ExeExt* = ""
|
||||
ScriptExt* = ""
|
||||
DynlibFormat* = "lib$1.so"
|
||||
|
||||
const
|
||||
ExtSep* = '.'
|
||||
|
||||
@@ -48,7 +48,8 @@ proc execProcess*(command: string,
|
||||
|
||||
proc execCmd*(command: string): int {.rtl, extern: "nosp$1".}
|
||||
## Executes ``command`` and returns its error code. Standard input, output,
|
||||
## error streams are inherited from the calling process.
|
||||
## error streams are inherited from the calling process. This operation
|
||||
## is also often called `system`:idx:.
|
||||
|
||||
proc startProcess*(command: string,
|
||||
workingDir: string = "",
|
||||
@@ -70,6 +71,17 @@ proc startProcess*(command: string,
|
||||
## Return value: The newly created process object. Nil is never returned,
|
||||
## but ``EOS`` is raised in case of an error.
|
||||
|
||||
proc startCmd*(command: string, options: set[TProcessOption] = {
|
||||
poStdErrToStdOut, poUseShell}): PProcess =
|
||||
## a simpler version of `startProcess` that parses the command line into
|
||||
## program and arguments and then calls `startProcess` with the empty string
|
||||
## for `workingDir` and the nil string table for `env`.
|
||||
var c = parseCmdLine(command)
|
||||
var a: seq[string]
|
||||
newSeq(a, c.len-1) # avoid slicing for now (still unstable)
|
||||
for i in 1 .. c.len-1: a[i-1] = c[i]
|
||||
result = startProcess(command=c[0], args=a, options=options)
|
||||
|
||||
proc close*(p: PProcess) {.rtl, extern: "nosp$1".}
|
||||
## When the process has finished executing, cleanup related handles
|
||||
|
||||
@@ -140,12 +152,6 @@ proc countProcessors*(): int {.rtl, extern: "nosp$1".} =
|
||||
result = sysconf(SC_NPROCESSORS_ONLN)
|
||||
if result <= 0: result = 1
|
||||
|
||||
proc startProcessAux(cmd: string, options: set[TProcessOption]): PProcess =
|
||||
var c = parseCmdLine(cmd)
|
||||
var a: seq[string] = @[] # slicing is not yet implemented :-(
|
||||
for i in 1 .. c.len-1: add(a, c[i])
|
||||
result = startProcess(command=c[0], args=a, options=options)
|
||||
|
||||
proc execProcesses*(cmds: openArray[string],
|
||||
options = {poStdErrToStdOut, poParentStreams},
|
||||
n = countProcessors()): int {.rtl, extern: "nosp$1".} =
|
||||
@@ -158,7 +164,7 @@ proc execProcesses*(cmds: openArray[string],
|
||||
newSeq(q, n)
|
||||
var m = min(n, cmds.len)
|
||||
for i in 0..m-1:
|
||||
q[i] = startProcessAux(cmds[i], options=options)
|
||||
q[i] = startCmd(cmds[i], options=options)
|
||||
when defined(noBusyWaiting):
|
||||
var r = 0
|
||||
for i in m..high(cmds):
|
||||
@@ -171,7 +177,7 @@ proc execProcesses*(cmds: openArray[string],
|
||||
echo(err)
|
||||
result = max(waitForExit(q[r]), result)
|
||||
if q[r] != nil: close(q[r])
|
||||
q[r] = startProcessAux(cmds[i], options=options)
|
||||
q[r] = startCmd(cmds[i], options=options)
|
||||
r = (r + 1) mod n
|
||||
else:
|
||||
var i = m
|
||||
@@ -182,7 +188,7 @@ proc execProcesses*(cmds: openArray[string],
|
||||
#echo(outputStream(q[r]).readLine())
|
||||
result = max(waitForExit(q[r]), result)
|
||||
if q[r] != nil: close(q[r])
|
||||
q[r] = startProcessAux(cmds[i], options=options)
|
||||
q[r] = startCmd(cmds[i], options=options)
|
||||
inc(i)
|
||||
if i > high(cmds): break
|
||||
for i in 0..m-1:
|
||||
@@ -190,7 +196,7 @@ proc execProcesses*(cmds: openArray[string],
|
||||
result = max(waitForExit(q[i]), result)
|
||||
else:
|
||||
for i in 0..high(cmds):
|
||||
var p = startProcessAux(cmds[i], options=options)
|
||||
var p = startCmd(cmds[i], options=options)
|
||||
result = max(waitForExit(p), result)
|
||||
close(p)
|
||||
|
||||
@@ -204,7 +210,7 @@ when not defined(useNimRtl):
|
||||
proc execProcess(command: string,
|
||||
options: set[TProcessOption] = {poStdErrToStdOut,
|
||||
poUseShell}): TaintedString =
|
||||
var p = startProcessAux(command, options=options)
|
||||
var p = startCmd(command, options=options)
|
||||
var outp = outputStream(p)
|
||||
result = TaintedString""
|
||||
while running(p) or not outp.atEnd(outp):
|
||||
@@ -625,6 +631,27 @@ elif not defined(useNimRtl):
|
||||
|
||||
pruneProcessSet(readfds, (rd))
|
||||
|
||||
|
||||
proc execCmdEx*(command: string, options: set[TProcessOption] = {
|
||||
poStdErrToStdOut, poUseShell}): tuple[
|
||||
output: TaintedString,
|
||||
exitCode: int] =
|
||||
## a convenience proc that runs the `command`, grabs all its output and
|
||||
## exit code and returns both.
|
||||
var p = startCmd(command, options)
|
||||
var outp = outputStream(p)
|
||||
result = (TaintedString"", -1)
|
||||
while not outp.atEnd(outp):
|
||||
result[0].string.add(outp.readLine().string)
|
||||
result[0].string.add("\n")
|
||||
result[1] = peekExitCode(p)
|
||||
if result[1] != -1: break
|
||||
outp.close(outp)
|
||||
if result[1] == -1:
|
||||
result[1] = peekExitCode(p)
|
||||
close(p)
|
||||
|
||||
|
||||
when isMainModule:
|
||||
var x = execProcess("gcc -v")
|
||||
echo "ECHO ", x
|
||||
|
||||
@@ -969,7 +969,7 @@ proc transformFile*(infile, outfile: string,
|
||||
## reads in the file `infile`, performs a parallel replacement (calls
|
||||
## `parallelReplace`) and writes back to `outfile`. Raises ``EIO`` if an
|
||||
## error occurs. This is supposed to be used for quick scripting.
|
||||
var x = readFile(infile)
|
||||
var x = readFile(infile).string
|
||||
writeFile(outfile, x.parallelReplace(subs))
|
||||
|
||||
iterator split*(s: string, sep: TPeg): string =
|
||||
|
||||
@@ -78,45 +78,49 @@ when defined(boehmgc):
|
||||
proc boehmRealloc(p: pointer, size: int): pointer {.
|
||||
importc: "GC_realloc", dynlib: boehmLib.}
|
||||
proc boehmDealloc(p: pointer) {.importc: "GC_free", dynlib: boehmLib.}
|
||||
|
||||
when not defined(useNimRtl):
|
||||
|
||||
proc alloc(size: int): pointer =
|
||||
result = boehmAlloc(size)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc alloc0(size: int): pointer =
|
||||
result = alloc(size)
|
||||
zeroMem(result, size)
|
||||
proc realloc(p: Pointer, newsize: int): pointer =
|
||||
result = boehmRealloc(p, newsize)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc dealloc(p: Pointer) = boehmDealloc(p)
|
||||
proc alloc(size: int): pointer =
|
||||
result = boehmAlloc(size)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc alloc0(size: int): pointer =
|
||||
result = alloc(size)
|
||||
zeroMem(result, size)
|
||||
proc realloc(p: Pointer, newsize: int): pointer =
|
||||
result = boehmRealloc(p, newsize)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc dealloc(p: Pointer) = boehmDealloc(p)
|
||||
|
||||
proc allocShared(size: int): pointer =
|
||||
result = boehmAlloc(size)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc allocShared0(size: int): pointer =
|
||||
result = alloc(size)
|
||||
zeroMem(result, size)
|
||||
proc reallocShared(p: Pointer, newsize: int): pointer =
|
||||
result = boehmRealloc(p, newsize)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc deallocShared(p: Pointer) = boehmDealloc(p)
|
||||
proc allocShared(size: int): pointer =
|
||||
result = boehmAlloc(size)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc allocShared0(size: int): pointer =
|
||||
result = alloc(size)
|
||||
zeroMem(result, size)
|
||||
proc reallocShared(p: Pointer, newsize: int): pointer =
|
||||
result = boehmRealloc(p, newsize)
|
||||
if result == nil: raiseOutOfMem()
|
||||
proc deallocShared(p: Pointer) = boehmDealloc(p)
|
||||
|
||||
#boehmGCincremental()
|
||||
|
||||
proc GC_disable() = boehmGC_disable()
|
||||
proc GC_enable() = boehmGC_enable()
|
||||
proc GC_fullCollect() = boehmGCfullCollect()
|
||||
proc GC_setStrategy(strategy: TGC_Strategy) = nil
|
||||
proc GC_enableMarkAndSweep() = nil
|
||||
proc GC_disableMarkAndSweep() = nil
|
||||
proc GC_getStatistics(): string = return ""
|
||||
|
||||
proc getOccupiedMem(): int = return -1
|
||||
proc getFreeMem(): int = return -1
|
||||
proc getTotalMem(): int = return -1
|
||||
|
||||
proc setStackBottom(theStackBottom: pointer) = nil
|
||||
|
||||
proc initGC() =
|
||||
when defined(macosx): boehmGCinit()
|
||||
|
||||
#boehmGCincremental()
|
||||
|
||||
proc GC_disable() = boehmGC_disable()
|
||||
proc GC_enable() = boehmGC_enable()
|
||||
proc GC_fullCollect() = boehmGCfullCollect()
|
||||
proc GC_setStrategy(strategy: TGC_Strategy) = nil
|
||||
proc GC_enableMarkAndSweep() = nil
|
||||
proc GC_disableMarkAndSweep() = nil
|
||||
proc GC_getStatistics(): string = return ""
|
||||
|
||||
proc getOccupiedMem(): int = return -1
|
||||
proc getFreeMem(): int = return -1
|
||||
proc getTotalMem(): int = return -1
|
||||
|
||||
proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
|
||||
result = alloc(size)
|
||||
@@ -128,7 +132,6 @@ when defined(boehmgc):
|
||||
proc growObj(old: pointer, newsize: int): pointer =
|
||||
result = realloc(old, newsize)
|
||||
|
||||
proc setStackBottom(theStackBottom: pointer) = nil
|
||||
proc nimGCref(p: pointer) {.compilerproc, inline.} = nil
|
||||
proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user