Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #20611 - Adds findExeAll to os #21578

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from

Conversation

bob16795
Copy link

No description provided.

@bob16795 bob16795 changed the title Fixes #20611 Implements #20611 - Adds findExeAll to os Mar 30, 2023
lib/pure/os.nim Outdated
result = iterator() : string =
template checkCurrentDir() =
for ext in extensions:
var exe_with_ext = addFileExt(exe, ext)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Nim naming conventions, for stdlib at least.

lib/pure/os.nim Outdated
@@ -219,10 219,11 @@ const
ExeExts* = ## Platform specific file extension for executables.
## On Windows ``["exe", "cmd", "bat"]``, on Posix ``[""]``.
when defined(windows): ["exe", "cmd", "bat"] else: [""]
ExeExtsSeq: seq[string] = toSeq(ExeExts)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this line?, openArray[string] can take array and seq.

lib/pure/os.nim Outdated
x = r
checkCurrentDir()
let path = getEnv("PATH")
for candidate in split(path, PathSep):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split should use maxsplit ?.

lib/pure/os.nim Outdated
var r = newString(maxSymlinkLen)
var len = readlink(x.cstring, r.cstring, maxSymlinkLen)
if len < 0:
raiseOSError(osLastError(), exe)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osLastError ?.

lib/pure/os.nim Outdated
if len < 0:
raiseOSError(osLastError(), exe)
if len > maxSymlinkLen:
r = newString(len 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use spaces around operators, at least for stdlib.

lib/pure/os.nim Outdated
when defined(windows):
var x = (if candidate[0] == '"' and candidate[^1] == '"':
substr(candidate, 1, candidate.len-2) else: candidate) /
exe
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way kinda copies the string too much ?.

lib/pure/os.nim Outdated
if fileExists(exe_with_ext):
yield exe_with_ext
when defined(posix):
if '/' in exe: checkCurrentDir()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So exe="/foo.bin" checks current directory ?.

lib/pure/os.nim Outdated
## meets the actual file. This behavior can be disabled if desired
## by setting `followSymlinks = false`.

var results : seq[string]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use result instead.

lib/pure/os.nim Outdated
let exes = iterExes(exe, followSymlinks, extensions)
for exe in exes():
results.add(exe)
return results
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to return if you just assign to result.

##
## If the system supports symlinks it also resolves them until it
## meets the actual file. This behavior can be disabled if desired
## by setting `followSymlinks = false`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some kind of max limit integer Positive for safety.

I can just touch and chmod x a ton of files on a folder,
and the function will hang the system for years...

lib/pure/os.nim Outdated
var x = addFileExt(x, ext)
if fileExists(x):
when not (defined(windows) or defined(nintendoswitch)):
while followSymlinks: # doubles as if here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" while doubles as if " ? 🤔
Why is that needed anyways.

if isAbsolute(r):
x = r
checkCurrentDir()
let path = getEnv("PATH")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path can be empty string.

## listed in the PATH environment variable. Returns "" if the exe cannot be
## found.
builtin

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you actually confirmed all this works on Nimscript ?.

lib/pure/os.nim Outdated
break
yield x

proc findExe*(exe: string, followSymlinks: bool=true, maxChecks=1000,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxChecks = 1000.Positive IMHO.

(Positive is not perfect for advanced users, but still better than just plain int because it wont allow negative values)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants