Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- get make_package so we can overwrite it
- local make_package = dofile("rom/modules/main/cc/require.lua").make
- -- make some upvalues
- -- tProgramStack is from the OG shell; keeps track of the stack of running programs
- local tProgramStack = {}
- local userPaths_mt = {
- __index=userPaths,
- __newindex=function() error("Use customPackage.addPackagePath() to add to the userPaths list.", 2) end,
- __metatable="Dude, what are you trying to do?"
- }
- -- make a global variable we can access from any program
- _G.customPackage = {
- userPaths = setmetatable({}, userPaths_mt)
- }
- --
- customPackage.addPackagePath = function(path)
- if not fs.isDir(path) then
- error(path.." is not a directory",2)
- end
- if path:sub(1,1) ~= "/" then
- error(path.." is not absolute (Make sure there's a leading slash)",2)
- end
- -- add trailing slash if there isn't one
- rawset(customPackage.userPaths, #customPackage.userPaths + 1, (path:sub(-1,-1) == "/") and path or path.."/")
- end
- local function createShellEnv(dir)
- local env = { shell = shell, multishell = multishell }
- env.require, env.package = make_package(env, dir)
- return env
- end
- function shell.execute(command, ...)
- local sPath = shell.resolveProgram(command)
- if sPath ~= nil then
- tProgramStack[#tProgramStack + 1] = sPath
- if multishell then
- local sTitle = fs.getName(sPath)
- if sTitle:sub(-4) == ".lua" then
- sTitle = sTitle:sub(1, -5)
- end
- multishell.setTitle(multishell.getCurrent(), sTitle)
- end
- local sDir = fs.getDir(sPath)
- local env = createShellEnv(sDir)
- env.arg = { [0] = command, ... }
- for _, path in ipairs(customPackage.userPaths) do
- env.package.path = path.."?;"..path.."?.lua;"..path.."?/init.lua;"..env.package.path
- end
- local result = os.run(env, sPath, ...)
- tProgramStack[#tProgramStack] = nil
- if multishell then
- if #tProgramStack > 0 then
- local sTitle = fs.getName(tProgramStack[#tProgramStack])
- if sTitle:sub(-4) == ".lua" then
- sTitle = sTitle:sub(1, -5)
- end
- multishell.setTitle(multishell.getCurrent(), sTitle)
- else
- multishell.setTitle(multishell.getCurrent(), "shell")
- end
- end
- return result
- else
- printError("No such program")
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement