Skip to content

Commit

Permalink
Implement environment variable passing for Process on Win32. Closes #790
Browse files Browse the repository at this point in the history
. Closes #792
  • Loading branch information
fasterthanlime committed Aug 17, 2014
1 parent ccf0b80 commit 434853a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
38 changes: 37 additions & 1 deletion sdk/os/native/ProcessWin32.ooc
Expand Up @@ -76,6 +76,8 @@ ProcessWin32: class extends Process {
si flags |= StartFlags UseStdHandles
}

envString := buildEnvironment()

// Reference: http://msdn.microsoft.com/en-us/library/ms682512%28VS.85%29.aspx
// Start the child process.
if(!CreateProcess(
Expand All @@ -85,7 +87,7 @@ ProcessWin32: class extends Process {
null, // Thread handle not inheritable
true, // Set handle inheritance to true
0, // No creation flags
null, // Use parent's environment block
envString, // Use parent's environment block
cwd ? cwd toCString() : null, // Use custom cwd if we have one
si&, // Pointer to STARTUPINFO structure
pi& // Pointer to PROCESS_INFORMATION structure
Expand Down Expand Up @@ -114,6 +116,40 @@ ProcessWin32: class extends Process {
return pi pid
}

buildEnvironment: func -> Char* {
if (env == null) {
return null
}

envLength := 1
env each(|k, v|
envLength += k size
envLength += v size
envLength += 2 // one for the =, one for the \0
)

envString := gc_malloc(envLength) as Char*
index := 0
for (k in env getKeys()) {
v := env get(k)

memcpy(envString + index, k toCString(), k size)
index += k size

envString[index] = '='
index += 1

memcpy(envString + index, v toCString(), v size)
index += v size

envString[index] = '\0'
index += 1
}

envString[index] = '\0'
envString
}

terminate: func {
// TODO!
"please implement me! ProcessWin32 terminate" println()
Expand Down
25 changes: 18 additions & 7 deletions test/sdk/os/processenv.ooc
@@ -1,12 +1,23 @@
import os/Process, structs/HashMap

p := Process new(["bash", "-c", "echo $MYVAR"])
main: func {
env := HashMap<String, String> new()
env put("MYVAR", "42")

env := HashMap<String, String> new()
env put("MYVAR", "42")
p setEnv(env)
command := ["bash", "-c", "echo $MYVAR"]
version (windows) {
command = ["cmd", "/c", "echo %MYVAR%"]
}

p execute()
output := p getOutput()
p := Process new(command)
p setEnv(env)
p execute()
output := p getOutput()
output = output trim()
if (output != "42") {
"Fail! expected output = 42, but got #{output}" println()
exit(1)
}

"output = %s" printfln(output)
"Pass" println()
}

0 comments on commit 434853a

Please sign in to comment.