Skip to content

Commit 76172d0

Browse files
committedApr 22, 2015
Made env variables local (i.e. os.getenv/setenv will operate per process now), to avoid programs running in parallel (via piping) to interfere.
Copying env vars from last process to shell's env to avoid breaking compat. (cd.lua will still work that way, e.g.) Closes #634.

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed
 

Diff for: ‎src/main/resources/assets/opencomputers/loot/OpenOS/bin/sh.lua

+10-3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ local function execute(env, command, ...)
266266
local program, args, input, output, mode = table.unpack(commands[i])
267267
local reason
268268
threads[i], reason = process.load(program, env, function()
269+
os.setenv("_", program)
269270
if input then
270271
local file, reason = io.open(shell.resolve(input))
271272
if not file then
@@ -304,9 +305,6 @@ local function execute(env, command, ...)
304305
return false, reason
305306
end
306307

307-
-- TODO needs moving of env vars into process lib/info
308-
-- os.setenv("_", program)
309-
310308
if i < #commands then
311309
pipes[i] = require("buffer").new("rw", memoryStream.new())
312310
pipes[i]:setvbuf("no")
@@ -351,6 +349,15 @@ local function execute(env, command, ...)
351349
end
352350
end
353351

352+
-- copy env vars from last process; mostly to ensure stuff like cd.lua works
353+
local lastVars = rawget(process.info(threads[#threads]).data, "vars")
354+
if lastVars then
355+
local localVars = process.info().data.vars
356+
for k,v in pairs(lastVars) do
357+
localVars[k] = v
358+
end
359+
end
360+
354361
for _, input in ipairs(inputs) do
355362
input:close()
356363
end

Diff for: ‎src/main/resources/assets/opencomputers/loot/OpenOS/boot/02_os.lua

-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ local function env()
88
-- copy parent env when first requested; easiest way to keep things
99
-- like number of env vars trivial (#vars).
1010
local data = require("process").info().data
11-
--[[ TODO breaking change; will require set to be a shell built-in and
12-
may break other programs relying on setenv being global.
1311
if not rawget(data, "vars") then
1412
local vars = {}
1513
for k, v in pairs(data.vars or {}) do
1614
vars[k] = v
1715
end
1816
data.vars = vars
1917
end
20-
--]]
2118
data.vars = data.vars or {}
2219
return data.vars
2320
end

Diff for: ‎src/main/resources/assets/opencomputers/loot/OpenOS/lib/process.lua

+11-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ function process.running(level) -- kept for backwards compat, prefer process.inf
8383
end
8484
end
8585

86-
function process.info(level)
87-
level = level or 1
88-
local process = findProcess()
89-
while level > 1 and process do
90-
process = process.parent
91-
level = level - 1
86+
function process.info(levelOrThread)
87+
local process
88+
if type(levelOrThread) == "thread" then
89+
process = findProcess(levelOrThread)
90+
else
91+
local level = levelOrThread or 1
92+
process = findProcess()
93+
while level > 1 and process do
94+
process = process.parent
95+
level = level - 1
96+
end
9297
end
9398
if process then
9499
return {path=process.path, env=process.env, command=process.command, data=process.data}

0 commit comments

Comments
 (0)
Please sign in to comment.