Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = "1.0"
- local process = package.loaded.process
- local fs = package.loaded.filesystem
- local config
- local loaders = {}
- local lua_loader = {
- mode = "r",
- loadfile = function(path, env)
- return loadfile(path, "t", env)
- end,
- load = function(hand, env)
- local data = ""
- local lc = " "
- while lc and lc ~= "" do
- lc = hand:read(math.huge)
- data = data .. (lc or "")
- end
- hand:close()
- return load(data, "="..os.getenv("_"), "t", env)
- end,
- info = function()
- print(" The default Lua loader.")
- print(" ".._VERSION)
- end,
- version = "1.0",
- }
- function reload_config()
- if (not fs.exists("/etc/xsh.cfg")) then
- print("Creating configuration...")
- fs.makeDirectory("/etc/xsh")
- local h = io.open("/etc/xsh.cfg", "w")
- h:write("{\n loadorder = {\n \"lua\"\n }\n}")
- h:close()
- config = {loadorder={"lua"}}
- loaders = {}
- loaders.lua = lua_loader
- else
- local h = io.open("/etc/xsh.cfg", "r")
- config = load("return "..h:read("*a"))()
- h:close()
- loaders = {}
- loaders.lua = lua_loader
- for ent in fs.list("/etc/xsh") do
- loaders[ent:sub(1, #ent-4)] = dofile("/etc/xsh/"..ent)
- end
- end
- end
- print("Loading config...")
- reload_config()
- local function try_load(path, env)
- return function(...)
- local fs, shell = require("filesystem"), require("shell")
- local ext = path:match("%.(.+)$")
- --print(ext)
- if (ext) then
- local prog = shell.resolve(path)
- if (prog) then
- os.setenv("_", prog)
- local f = fs.open(prog, loaders[ext].mode)
- --print("Running program "..prog.." with loader "..config.loadorder[i])
- return assert(loaders[ext].load(f, env))(...)
- else
- return require("tools/programLocations").reportNotFound(path, "not found")
- end
- end
- for i=1, #config.loadorder do
- local prog = shell.resolve(path, config.loadorder[i])
- --print("Trying loader: "..config.loadorder[i])
- if (prog) then
- os.setenv("_", prog)
- local f = fs.open(prog, loaders[config.loadorder[i]].mode)
- --print("Running program "..prog.." with loader "..config.loadorder[i])
- return assert(loaders[config.loadorder[i]].load(f, env))(...)
- end
- end
- return require("tools/programLocations").reportNotFound(path, "not found")
- end
- end
- local oload = process.load
- local function proc_load(path, env, init, name)
- checkArg(1, path, "string", "function")
- checkArg(2, env, "table", "nil")
- checkArg(3, init, "function", "nil")
- checkArg(4, name, "string", "nil")
- assert(type(path) == "string" or env == nil, "process cannot load function environments")
- local p = process.findProcess()
- env = env or p.env
- local code
- if type(path) == "string" then
- if (path == "xshinfo") then
- code = function(...)
- print("xSH - eXtendable SHell")
- print("Easy shell extensions for OpenOS.\n")
- print("xSH version: "..version)
- print("OpenOS version: ".._OSVERSION)
- print("Lua version: ".._VERSION)
- print("Load order:\n - "..table.concat(config.loadorder, "\n - "))
- for k, v in pairs(loaders) do
- print(k .. " version: "..v.version)
- if (v.info) then
- v.info()
- end
- end
- return 0
- end
- else
- code = try_load(path, env)
- end
- else -- path is code
- code = path
- end
- local thread = nil
- thread = coroutine.create(function(...)
- -- pcall code so that we can remove it from the process list on exit
- local result =
- {
- xpcall(function(...)
- init = init or function(...) return ... end
- return code(init(...))
- end,
- function(msg)
- if type(msg) == "table" and msg.reason == "terminated" then
- return msg.code or 0
- end
- local stack = debug.traceback():gsub("^([^\n]*\n)[^\n]*\n[^\n]*\n","%1")
- io.stderr:write(string.format("%s:\n%s", msg or "", stack))
- return 128 -- syserr
- end, ...)
- }
- --result[1] is false if the exception handler also crashed
- if not result[1] and type(result[2]) ~= "number" then
- pcall(require("event").onError, string.format("process library exception handler crashed: %s", tostring(result[2])))
- end
- -- onError opens a file, you can't open a file without a process, we close the process last
- process.internal.close(thread, result)
- return select(2, table.unpack(result))
- end, true)
- local new_proc =
- {
- path = path,
- command = name or tostring(path),
- env = env,
- data =
- {
- handles = {},
- io = {},
- },
- parent = p,
- instances = setmetatable({}, {__mode="v"}),
- }
- for i,fd in pairs(p.data.io) do
- new_proc.data.io[i] = io.dup(fd)
- end
- setmetatable(new_proc.data, {__index=p.data})
- process.list[thread] = new_proc
- return thread
- end
- local loaded = package.loaded
- local loading = {}
- local orequire = require
- local function n_require(module)
- checkArg(1, module, "string")
- if loaded[module] ~= nil then
- return loaded[module]
- elseif not loading[module] then
- local library, status, step
- step, library, status = "not found", package.searchpath(module, package.path)
- --print(library, library:match("%.(.+)$"))
- if library then
- step, library, status = "loadfile failed", loaders[library:match("%.(.+)$")].loadfile(library)
- end
- if library then
- loading[module] = true
- step, library, status = "load failed", pcall(library, module)
- loading[module] = false
- end
- assert(library, string.format("module '%s' %s:\n%s", module, step, status))
- loaded[module] = status
- return status
- else
- error("already loading: " .. module .. "\n" .. debug.traceback(), 2)
- end
- end
- function start()
- _G.require = n_require
- process.load = proc_load
- _G._XSH = true
- end
- function stop()
- _G.require = orequire
- process.load = oload
- _G._XSH = nil
- end
Add Comment
Please, Sign In to add comment