hare

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

commit 4254f1ce78362b96d08bcbbba77e0647380b62e6
parent 4d790c7248e1561c6bb03ba3930f9a2673b34211
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat,  6 Mar 2021 11:35:30 -0500

os::exec: pull environment from parent proc

Diffstat:
Mos/+linux/environ.ha | 13+++++++++++++
Mos/exec/cmd.ha | 9+++++++++
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/os/+linux/environ.ha b/os/+linux/environ.ha @@ -47,3 +47,16 @@ export fn getenv(name: const str) (str | void) = { }; }; }; + +let envp: []str = []; + +// Returns a slice of the environment strings in the form KEY=VALUE. +export fn getenvs() []str = { + if (len(envp) != 0) { + return envp; + }; + for (let i = 0z; rt::envp[i] != null; i += 1) { + append(envp, strings::from_c(rt::envp[i]: *const char)); + }; + return envp; +}; diff --git a/os/exec/cmd.ha b/os/exec/cmd.ha @@ -15,6 +15,7 @@ use strings; // // By default, the new command will inherit the current process's environment. export fn cmd(name: str, args: str...) (command | error) = { + let env = os::getenvs(); let cmd = command { platform: platform_cmd = if (strings::contains(name, '/')) match (open(name)) { @@ -25,9 +26,11 @@ export fn cmd(name: str, args: str...) (command | error) = { p: platform_cmd => p, }, argv = alloc([], len(args) + 1z), + envp = alloc([], len(env)), ... }; append(cmd.argv, name, ...args); + append(cmd.envp, ...env); return cmd; }; @@ -55,6 +58,12 @@ export fn start(cmd: *command) (error | process) = { }; }; +// Empties the environment variables for the command. By default, the command +// inherits the environment of the parent process. +export fn clearenv(cmd: *command) void = { + cmd.envp = []; +}; + fn lookup(name: str) (platform_cmd | void) = { const path = match (os::getenv("PATH")) { void => return,