Advertisement
BigSHinyToys

[Computer Craft] multi tasking

Nov 28th, 2012
683
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" -- curretn vershion
  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. local function customFileSystem(root)
  40.     local newFS = {}
  41.     local backUpFS = fs
  42.     for k,v in pairs(fs) do
  43.         newFS[k] = function(path,...)
  44.             return backUpFS[k](root..path,...)
  45.         end
  46.     end
  47.     return newFS
  48. end
  49.  
  50. local function customFileSystemIO(root)
  51.     local newFS = {}
  52.     local backUpFS = io
  53.     for k,v in pairs(io) do
  54.         newFS[k] = function(path,...)
  55.             return backUpFS[k](root..path,...)
  56.         end
  57.     end
  58.     return newFS
  59. end
  60.  
  61. local function customRun(root)
  62.     local old = os.run
  63.     return function(env,path,...)
  64.         old(env,root..path,...)
  65.     end
  66. end
  67.  
  68. -- sysLog saves the string it is sent to a file
  69. -- this saves the reason why a thread crashed
  70. local function sysLog(sDATA)
  71.     file = fs.open(logFile,"a")
  72.     if file then
  73.         file.write(sDATA.."\n")
  74.         file:close()
  75.         return true
  76.     else
  77.         return false
  78.     end
  79. end
  80.  
  81.  
  82. -- this returns a list of running threads
  83. local function listThread()
  84.     local list = {}
  85.     for i = 1,#tThreads do
  86.         table.insert(list,tThreads[i].parth)
  87.     end
  88.     return list
  89. end
  90.  
  91. -- this function makes a new thread
  92. local function newThread(...)
  93.     sysLog("Thread Request :") -- saves a message to the log file
  94.     local input = {...} -- puts sent arguments in table
  95.     local tEnv = deepcopy(oldG) -- makes a local copy of the global table
  96.     local root = "SYS_MOUSE/users/admin" -- for fs permitons this is the
  97.     if input[1] == false then -- checks if fs permitions are granted false for limmited permitions
  98.         -- this section modifies the copy of the _G table adding custom replacments for certin functions
  99.         tEnv.fs = customFileSystem(root)
  100.         tEnv.shell = {}
  101.         tEnv.shell.resolve = function(path) return path end
  102.         tEnv.shell.getRuningProgram = function() return root end
  103.         tEnv.io = customFileSystemIO(root)
  104.         tEnv.os.run = customRun(root)
  105.     end
  106.     -- this allows a program to creat a new thread
  107.     tEnv.os.newThread = newThread
  108.     -- this allows a program to get a list of running programs
  109.     tEnv.os.listThread = listThread
  110.     -- removes the first value
  111.     table.remove(input,1)
  112.     -- this section checks for the existance of the program
  113.     if input[1] and fs.exists(input[1]) then
  114.         sysLog("New Thread : "..tostring(input[1])) -- logs that a new thread was requested
  115.         local program = input[1]
  116.         table.remove(input,1)
  117.         -- adds the new thread to the thread table
  118.         table.insert(
  119.             tThreads,{
  120.                 parth = tostring(program),
  121.                 env = deepcopy(tEnv), -- copys the newly modified copy of the _G
  122.                 main = coroutine.create(function(...) return os.run(tThreads[#tThreads].env,program,...) end)
  123.             }
  124.         )
  125.         sysLog("Thread Made : ") -- logs if it worked or not
  126.         sysLog("Input : "..#input)
  127.         local data = {}
  128.        
  129.         for i = 1,#input do
  130.             table.insert(data,tostring(input[i]))
  131.         end
  132.         sysLog(table.concat(data,"\n"))
  133.         -- sends in the argmetns to the program
  134.         local test = {coroutine.resume(tThreads[#tThreads].main,unpack(input))}
  135.         sysLog("First Output : ")
  136.         data = {}
  137.         for i = 1,#test do
  138.             table.insert(data,tostring(test[i]))
  139.         end
  140.         sysLog(table.concat(data," "))
  141.         sysLog("-- end Thread request --")
  142.     end
  143. end
  144.  
  145. sysLog("---- New log ----") -- logs that the program started
  146.  
  147. newThread(true,"SYS_MOUSE/desk_top.lua") -- calls the creation of a new thread running the program specified
  148.  
  149. while true do -- main loop
  150.     local event = {coroutine.yield()} -- non terminataible this is basicly os.pullEvent() and it is put into a tbale
  151.     local pos = 1
  152.     while pos <= #tThreads do -- go thorught the list of threads
  153.         local test , req = coroutine.resume(tThreads[pos].main,unpack(event)) -- execute the thread with the event
  154.         if coroutine.status(tThreads[pos].main) ~= "dead" and test then -- if thread not dead then go to the next thread
  155.             pos = pos + 1
  156.         else -- else remove the thread from the table and log the error
  157.             sysLog("Thread Crash "..#tThreads.." : "..coroutine.status(tThreads[pos].main).." "..tostring(tThreads[pos].parth).." "..tostring(req))
  158.             table.remove(tThreads,pos)
  159.             if #tThreads == 0 then -- if everthing crashes then go back to shell after pressing enter
  160.                 local evnt,rand -- tempo values for testing
  161.                 while rand ~= 28 do
  162.                     evnt,rand = os.pullEvent("key")
  163.                 end
  164.                 term.setBackgroundColor(colors.black)
  165.                 term.setTextColor(colors.white)
  166.                 term.clear()
  167.                 term.setCursorPos(1,1)
  168.                 print("System Crash loading old Shell")
  169.                 return
  170.             end
  171.         end
  172.     end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement