findExe finds cmd and bat files; refs #5049
This commit is contained in:
parent
864946afb8
commit
36964d7491
1 changed files with 34 additions and 25 deletions
|
|
@ -562,43 +562,52 @@ when declared(getEnv) or defined(nimscript):
|
||||||
if lstat(path, rawInfo) < 0'i32: result = false
|
if lstat(path, rawInfo) < 0'i32: result = false
|
||||||
else: result = S_ISLNK(rawInfo.st_mode)
|
else: result = S_ISLNK(rawInfo.st_mode)
|
||||||
|
|
||||||
proc findExe*(exe: string, followSymlinks: bool = true): string {.
|
const
|
||||||
|
ExeExts* = when defined(windows): ["exe", "cmd", "bat"] else: [""] ## \
|
||||||
|
## platform specific file extension for executables. On Windows
|
||||||
|
## ``["exe", "cmd", "bat"]``, on Posix ``[""]``.
|
||||||
|
|
||||||
|
proc findExe*(exe: string, followSymlinks: bool = true;
|
||||||
|
extensions=ExeExts): string {.
|
||||||
tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect].} =
|
tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect].} =
|
||||||
## Searches for `exe` in the current working directory and then
|
## Searches for `exe` in the current working directory and then
|
||||||
## in directories listed in the ``PATH`` environment variable.
|
## in directories listed in the ``PATH`` environment variable.
|
||||||
## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe`
|
## Returns "" if the `exe` cannot be found. `exe`
|
||||||
## is added the `ExeExt <#ExeExt>`_ file extension if it has none.
|
## is added the `ExeExts <#ExeExts>`_ file extensions if it has none.
|
||||||
## If the system supports symlinks it also resolves them until it
|
## If the system supports symlinks it also resolves them until it
|
||||||
## meets the actual file. This behavior can be disabled if desired.
|
## meets the actual file. This behavior can be disabled if desired.
|
||||||
result = addFileExt(exe, ExeExt)
|
for ext in extensions:
|
||||||
if existsFile(result): return
|
result = addFileExt(exe, ext)
|
||||||
|
if existsFile(result): return
|
||||||
var path = string(getEnv("PATH"))
|
var path = string(getEnv("PATH"))
|
||||||
for candidate in split(path, PathSep):
|
for candidate in split(path, PathSep):
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
var x = (if candidate[0] == '"' and candidate[^1] == '"':
|
var x = (if candidate[0] == '"' and candidate[^1] == '"':
|
||||||
substr(candidate, 1, candidate.len-2) else: candidate) /
|
substr(candidate, 1, candidate.len-2) else: candidate) /
|
||||||
result
|
exe
|
||||||
else:
|
else:
|
||||||
var x = expandTilde(candidate) / result
|
var x = expandTilde(candidate) / exe
|
||||||
if existsFile(x):
|
for ext in extensions:
|
||||||
when not defined(windows) and declared(os):
|
let x = addFileExt(x, ext)
|
||||||
while followSymlinks: # doubles as if here
|
if existsFile(x):
|
||||||
if x.checkSymlink:
|
when not defined(windows) and declared(os):
|
||||||
var r = newString(256)
|
while followSymlinks: # doubles as if here
|
||||||
var len = readlink(x, r, 256)
|
if x.checkSymlink:
|
||||||
if len < 0:
|
var r = newString(256)
|
||||||
raiseOSError(osLastError())
|
var len = readlink(x, r, 256)
|
||||||
if len > 256:
|
if len < 0:
|
||||||
r = newString(len+1)
|
raiseOSError(osLastError())
|
||||||
len = readlink(x, r, len)
|
if len > 256:
|
||||||
setLen(r, len)
|
r = newString(len+1)
|
||||||
if isAbsolute(r):
|
len = readlink(x, r, len)
|
||||||
x = r
|
setLen(r, len)
|
||||||
|
if isAbsolute(r):
|
||||||
|
x = r
|
||||||
|
else:
|
||||||
|
x = parentDir(x) / r
|
||||||
else:
|
else:
|
||||||
x = parentDir(x) / r
|
break
|
||||||
else:
|
return x
|
||||||
break
|
|
||||||
return x
|
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
when defined(nimscript) or (defined(nimdoc) and not declared(os)):
|
when defined(nimscript) or (defined(nimdoc) and not declared(os)):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue