Skip to content

Commit

Permalink
Added env command
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Dec 3, 2019
1 parent aee943c commit ccf7e15
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ A Bourne-compatible shell for ComputerCraft.
### TODO
* Add test boolean operators (-a, -o)
* Add case statement
* Add /usr/bin/env equivalent

## License
This project is licensed under the MIT license. You are free to modify and redistribute cash.lua as long as the copyright notice is preserved at the top of the script.
11 changes: 10 additions & 1 deletion cash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local running = true
local shell_retval = 0
local shell_title = nil
local execCommand
local shell_env = _ENV

local function splitFile(filename)
local file = io.open(filename, "r")
Expand Down Expand Up @@ -568,6 +569,14 @@ function multishell.getFocus()
return 1
end

function shell.environment()
return shell_env
end

function shell.setEnvironment(e)
shell_env = e
end

local function expandVar(var)
if string.sub(var, 1, 1) ~= "$" then return nil end
if string.sub(var, 2, 2) == "{" then
Expand Down Expand Up @@ -827,7 +836,7 @@ local function execv(tokens)
end
local _old = vars._
vars._ = path
run(setmetatable({shell = shell, multishell = multishell, package = pack, require = require}, {__index = _ENV}), path, table.unpack(tokens))
run(setmetatable({shell = shell, multishell = multishell, package = pack, require = require}, {__index = shell_env}), path, table.unpack(tokens))
vars._ = _old
end
for k,v in pairs(tokens.vars) do _ENV[k] = oldenv[k] end
Expand Down
39 changes: 39 additions & 0 deletions env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local args = {...}

local env = setmetatable({}, {__index = _ENV})
local path = shell.path()
local unset = {}
local cmd = {}
local cmdargs = false
local nextarg

for k,v in ipairs(args) do
if nextarg then
if nextarg == 1 then path = v
elseif nextarg == 2 then table.insert(unset, v) end
nextarg = nil
elseif cmdargs then
table.insert(cmd, v)
else
if v == "-i" then setmetatable(env, {__index = _G})
elseif v == "-P" then nextarg = 1
elseif v == "-u" then nextarg = 2
elseif string.find(v, "=") then env[string.sub(v, 1, string.find(v, "=") - 1)] = string.sub(v, string.find(v, "=") + 1)
else table.insert(cmd, v); cmdargs = true end
end
end

if #unset > 0 then
local oldidx = getmetatable(env).__index
local u = {}
for k,v in ipairs(unset) do u[v] = true end
setmetatable(env, {__index = function(self, name) if u[name] then return nil else return oldidx[name] end end})
end

local oldPath = shell.path()
local oldEnv = shell.environment()
shell.setPath(path)
shell.setEnvironment(env)
shell.run(table.unpack(cmd))
shell.setPath(oldPath)
shell.setEnvironment(oldEnv)

0 comments on commit ccf7e15

Please sign in to comment.