Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- simple kernal
- with FS permitions / enviromant control
- by BigSHinyToys
- notes: extentions that will be suported
- .lnk < shourcuts
- .txt < text files
- .lua < executible lua script
- .cnf < configur file
- ]]--
- local ver = "0.2" -- curretn vershion
- function deepcopy(orig) -- modified deep copy function from
- local copy
- if type(orig) == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- if orig_key ~= "_G" then
- copy[deepcopy(orig_key)] = deepcopy(orig_value)
- end
- end
- setmetatable(copy, deepcopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- local oldG = deepcopy(_G) -- makes copy of the _G table
- local logFile = "sysErrorLog" -- file for errors to be loged
- local tThreads = {} -- this table stored all the threads coroutienes
- local focused = 1 -- curretnly not used will be for focuesed routine
- --[[
- the bellow functions make clones of the original function
- returning a modified ver hat has permituions enforced
- ]]--
- local function customFileSystem(root)
- local newFS = {}
- local backUpFS = fs
- for k,v in pairs(fs) do
- newFS[k] = function(path,...)
- return backUpFS[k](root..path,...)
- end
- end
- return newFS
- end
- local function customFileSystemIO(root)
- local newFS = {}
- local backUpFS = io
- for k,v in pairs(io) do
- newFS[k] = function(path,...)
- return backUpFS[k](root..path,...)
- end
- end
- return newFS
- end
- local function customRun(root)
- local old = os.run
- return function(env,path,...)
- old(env,root..path,...)
- end
- end
- -- sysLog saves the string it is sent to a file
- -- this saves the reason why a thread crashed
- local function sysLog(sDATA)
- file = fs.open(logFile,"a")
- if file then
- file.write(sDATA.."\n")
- file:close()
- return true
- else
- return false
- end
- end
- -- this returns a list of running threads
- local function listThread()
- local list = {}
- for i = 1,#tThreads do
- table.insert(list,tThreads[i].parth)
- end
- return list
- end
- -- this function makes a new thread
- local function newThread(...)
- sysLog("Thread Request :") -- saves a message to the log file
- local input = {...} -- puts sent arguments in table
- local tEnv = deepcopy(oldG) -- makes a local copy of the global table
- local root = "SYS_MOUSE/users/admin" -- for fs permitons this is the
- if input[1] == false then -- checks if fs permitions are granted false for limmited permitions
- -- this section modifies the copy of the _G table adding custom replacments for certin functions
- tEnv.fs = customFileSystem(root)
- tEnv.shell = {}
- tEnv.shell.resolve = function(path) return path end
- tEnv.shell.getRuningProgram = function() return root end
- tEnv.io = customFileSystemIO(root)
- tEnv.os.run = customRun(root)
- end
- -- this allows a program to creat a new thread
- tEnv.os.newThread = newThread
- -- this allows a program to get a list of running programs
- tEnv.os.listThread = listThread
- -- removes the first value
- table.remove(input,1)
- -- this section checks for the existance of the program
- if input[1] and fs.exists(input[1]) then
- sysLog("New Thread : "..tostring(input[1])) -- logs that a new thread was requested
- local program = input[1]
- table.remove(input,1)
- -- adds the new thread to the thread table
- table.insert(
- tThreads,{
- parth = tostring(program),
- env = deepcopy(tEnv), -- copys the newly modified copy of the _G
- main = coroutine.create(function(...) return os.run(tThreads[#tThreads].env,program,...) end)
- }
- )
- sysLog("Thread Made : ") -- logs if it worked or not
- sysLog("Input : "..#input)
- local data = {}
- for i = 1,#input do
- table.insert(data,tostring(input[i]))
- end
- sysLog(table.concat(data,"\n"))
- -- sends in the argmetns to the program
- local test = {coroutine.resume(tThreads[#tThreads].main,unpack(input))}
- sysLog("First Output : ")
- data = {}
- for i = 1,#test do
- table.insert(data,tostring(test[i]))
- end
- sysLog(table.concat(data," "))
- sysLog("-- end Thread request --")
- end
- end
- sysLog("---- New log ----") -- logs that the program started
- newThread(true,"SYS_MOUSE/desk_top.lua") -- calls the creation of a new thread running the program specified
- while true do -- main loop
- local event = {coroutine.yield()} -- non terminataible this is basicly os.pullEvent() and it is put into a tbale
- local pos = 1
- while pos <= #tThreads do -- go thorught the list of threads
- local test , req = coroutine.resume(tThreads[pos].main,unpack(event)) -- execute the thread with the event
- if coroutine.status(tThreads[pos].main) ~= "dead" and test then -- if thread not dead then go to the next thread
- pos = pos + 1
- else -- else remove the thread from the table and log the error
- sysLog("Thread Crash "..#tThreads.." : "..coroutine.status(tThreads[pos].main).." "..tostring(tThreads[pos].parth).." "..tostring(req))
- table.remove(tThreads,pos)
- if #tThreads == 0 then -- if everthing crashes then go back to shell after pressing enter
- local evnt,rand -- tempo values for testing
- while rand ~= 28 do
- evnt,rand = os.pullEvent("key")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("System Crash loading old Shell")
- return
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement