hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 13f3fbfa2f87c6ae1f8521ad7a28b6d02fc85c3f
parent 8d7454a23348df1ac931e898b7d2efd88747621a
Author: illiliti <illiliti@dimension.sh>
Date:   Mon,  6 Feb 2023 05:20:36 +0300

os::exec: add cmdfile function

Can be used to bypass PATH lookup and/or execute files from memory.

Signed-off-by: illiliti <illiliti@dimension.sh>

Diffstat:
Mos/exec/cmd.ha | 37+++++++++++++++++++++----------------
1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/os/exec/cmd.ha b/os/exec/cmd.ha @@ -22,23 +22,28 @@ use strings; // // By default, the new command will inherit the current process's environment. export fn cmd(name: str, args: str...) (command | error) = { + let file = if (strings::contains(name, '/')) { + yield match (open(name)) { + case let p: platform_cmd => + yield p; + case => + return nocmd; + }; + } else { + yield match (lookup(name)?) { + case void => + return nocmd; + case let p: platform_cmd => + yield p; + }; + }; + return cmdfile(file, name, args...); +}; + +// Same as [[cmd]] except that executable file is determined by [[io::file]]. +export fn cmdfile(file: io::file, name: str, args: str...) command = { let cmd = command { - platform: platform_cmd = - if (strings::contains(name, '/')) { - yield match (open(name)) { - case let p: platform_cmd => - yield p; - case => - return nocmd; - }; - } else { - yield match (lookup(name)?) { - case void => - return nocmd; - case let p: platform_cmd => - yield p; - }; - }, + platform = file, argv = alloc([], len(args) + 1), env = strings::dupall(os::getenvs()), files = [],