Advertisement
BigSHinyToys

multi tasking

Jan 12th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. --[[
  2.         simple kernal
  3.         with FS permitions / enviromant control
  4.         by BigSHinyToys
  5.        
  6.     notes: extentions that will be suported
  7.     .lnk < shourcuts
  8.     .txt < text files
  9.     .lua < executible lua script
  10.     .cnf < configur file
  11. ]]--
  12. local ver = "0.2" -- current vershon
  13.  
  14. function deepcopy(orig) -- modified deep copy function from
  15.     local copy
  16.     if type(orig) == 'table' then
  17.         copy = {}
  18.         for orig_key, orig_value in next, orig, nil do
  19.             if orig_key ~= "_G" then
  20.                 copy[deepcopy(orig_key)] = deepcopy(orig_value)
  21.             end
  22.         end
  23.         setmetatable(copy, deepcopy(getmetatable(orig)))
  24.     else -- number, string, boolean, etc
  25.         copy = orig
  26.     end
  27.     return copy
  28. end
  29.  
  30. local oldG = deepcopy(_G) -- makes copy of the _G table
  31. local logFile = "sysErrorLog" -- file for errors to be loged
  32. local tThreads = {} -- this table stored all the threads coroutienes
  33. local focused = 1 -- curretnly not used will be for focuesed routine
  34.  
  35. --[[
  36. the bellow functions make clones of the original function
  37. returning a modified ver hat has permituions enforced
  38. ]]--
  39.  
  40. local function customFileSystem(root)
  41.     local newFS = {}
  42.     local backUpFS = fs
  43.     for k,v in pairs(fs) do
  44.         newFS[k] = function(path,...)
  45.             return backUpFS[k](root..path,...)
  46.         end
  47.     end
  48.     return newFS
  49. end
  50.  
  51. local function customFileSystemIO(root)
  52.     local newFS = {}
  53.     local backUpFS = io
  54.     for k,v in pairs(io) do
  55.         newFS[k] = function(path,...)
  56.             return backUpFS[k](root..path,...)
  57.         end
  58.     end
  59.     return newFS
  60. end
  61.  
  62. local function customRun(root)
  63.     local old = os.run
  64.     return function(env,path,...)
  65.         old(env,root..path,...)
  66.     end
  67. end
  68.  
  69. -- sysLog saves the string it is sent to a file
  70. -- this saves the reason why a thread crashed
  71. local function sysLog(sDATA)
  72.     file = fs.open(logFile,"a")
  73.     if file then
  74.         file.write(sDATA.."\n")
  75.         file:close()
  76.         return true
  77.     else
  78.         return false
  79.     end
  80. end
  81.  
  82.  
  83. -- this returns a list of running threads
  84. local function listThread()
  85.     local list = {}
  86.     for i = 1,#tThreads do
  87.         table.insert(list,tThreads[i].parth)
  88.     end
  89.     return list
  90. end
  91.  
  92. -- this function makes a new thread
  93. local function newThread(...)
  94.     sysLog("Thread Request :") -- saves a message to the log file
  95.     local input = {...} -- puts sent arguments in table
  96.     local tEnv = deepcopy(oldG) -- makes a local copy of the global table
  97.     local root = "SYS_MOUSE/users/admin" -- for fs permitons this is the
  98.     if input[1] == false then -- checks if fs permitions are granted false for limmited permitions
  99.         -- this section modifies the copy of the _G table adding custom replacments for certin functions
  100.         tEnv.fs = customFileSystem(root)
  101.         tEnv.shell = {}
  102.         tEnv.shell.resolve = function(path) return path end
  103.         tEnv.shell.getRuningProgram = function() return root end
  104.         tEnv.io = customFileSystemIO(root)
  105.         tEnv.os.run = customRun(root)
  106.     end
  107.     -- this allows a program to creat a new thread
  108.     tEnv.os.newThread = newThread
  109.     -- this allows a program to get a list of running programs
  110.     tEnv.os.listThread = listThread
  111.     -- removes the first value
  112.     table.remove(input,1)
  113.     -- this section checks for the existance of the program
  114.     if input[1] and fs.exists(input[1]) then
  115.         sysLog("New Thread : "..tostring(input[1])) -- logs that a new thread was requested
  116.         local program = input[1]
  117.         table.remove(input,1)
  118.         -- adds the new thread to the thread table
  119.         table.insert(
  120.             tThreads,{
  121.                 parth = tostring(program),
  122.                 env = deepcopy(tEnv), -- copys the newly modified copy of the _G
  123.                 main = coroutine.create(function(...) return os.run(tThreads[#tThreads].env,program,...) end)
  124.             }
  125.         )
  126.         sysLog("Thread Made : ") -- logs if it worked or not
  127.         sysLog("Input : "..#input)
  128.         local data = {}
  129.        
  130.         for i = 1,#input do
  131.             table.insert(data,tostring(input[i]))
  132.         end
  133.         sysLog(table.concat(data,"\n"))
  134.         -- sends in the argmetns to the program
  135.         local test = {coroutine.resume(tThreads[#tThreads].main,unpack(input))}
  136.         sysLog("First Output : ")
  137.         data = {}
  138.         for i = 1,#test do
  139.             table.insert(data,tostring(test[i]))
  140.         end
  141.         sysLog(table.concat(data," "))
  142.         sysLog("-- end Thread request --")
  143.     end
  144. end
  145.  
  146. sysLog("---- New log ----") -- logs that the program started
  147.  
  148. newThread(true,"SYS_MOUSE/desk_top.lua") -- calls the creation of a new thread running the program specified
  149.  
  150. while true do -- main loop
  151.     local event = {coroutine.yield()} -- non terminataible this is basicly os.pullEvent() and it is put into a tbale
  152.     local pos = 1
  153.     while pos <= #tThreads do -- go thorught the list of threads
  154.         local test , req = coroutine.resume(tThreads[pos].main,unpack(event)) -- execute the thread with the event
  155.         if coroutine.status(tThreads[pos].main) ~= "dead" and test then -- if thread not dead then go to the next thread
  156.             pos = pos + 1
  157.         else -- else remove the thread from the table and log the error
  158.             sysLog("Thread Crash "..#tThreads.." : "..coroutine.status(tThreads[pos].main).." "..tostring(tThreads[pos].parth).." "..tostring(req))
  159.             table.remove(tThreads,pos)
  160.             if #tThreads == 0 then -- if everthing crashes then go back to shell after pressing enter
  161.                 local evnt,rand -- tempo values for testing
  162.                 while rand ~= 28 do
  163.                     evnt,rand = os.pullEvent("key")
  164.                 end
  165.                 term.setBackgroundColor(colors.black)
  166.                 term.setTextColor(colors.white)
  167.                 term.clear()
  168.                 term.setCursorPos(1,1)
  169.                 print("System Crash loading old Shell")
  170.                 return
  171.             end
  172.         end
  173.     end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement