Derek1017

bios.lua

Jul 19th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.26 KB | None | 0 0
  1. -- Install lua parts of the os api
  2. function os.version()
  3.     if turtle then
  4.         return "TurtleOS 1.3"
  5.     end
  6.     return "CraftOS 1.3"
  7. end
  8.  
  9. function os.pullEventRaw( _sFilter )
  10.     return coroutine.yield( _sFilter )
  11. end
  12.  
  13. function os.pullEvent( _sFilter )
  14.     local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
  15.     if event == "terminate" then
  16.         print( "Terminated" )
  17.         error()
  18.     end
  19.     return event, p1, p2, p3, p4, p5
  20. end
  21.  
  22. -- Install globals
  23. function sleep( _nTime )
  24.     local timer = os.startTimer( _nTime )
  25.     repeat
  26.         local sEvent, param = os.pullEvent( "timer" )
  27.     until param == timer
  28. end
  29.  
  30. function write( sText )
  31.     local w,h = term.getSize()     
  32.     local x,y = term.getCursorPos()
  33.    
  34.     local nLinesPrinted = 0
  35.     local function newLine()
  36.         if y + 1 <= h then
  37.             term.setCursorPos(1, y + 1)
  38.         else
  39.             term.scroll(1)
  40.             term.setCursorPos(1, h)
  41.         end
  42.         x, y = term.getCursorPos()
  43.         nLinesPrinted = nLinesPrinted + 1
  44.     end
  45.    
  46.     -- Print the line with proper word wrapping
  47.     while string.len(sText) > 0 do
  48.         local whitespace = string.match( sText, "^[ \t]+" )
  49.         if whitespace then
  50.             -- Print whitespace
  51.             term.write( whitespace )
  52.             x,y = term.getCursorPos()
  53.             sText = string.sub( sText, string.len(whitespace) + 1 )
  54.         end
  55.        
  56.         local newline = string.match( sText, "^\n" )
  57.         if newline then
  58.             -- Print newlines
  59.             newLine()
  60.             sText = string.sub( sText, 2 )
  61.         end
  62.        
  63.         local text = string.match( sText, "^[^ \t\n]+" )
  64.         if text then
  65.             sText = string.sub( sText, string.len(text) + 1 )
  66.             if string.len(text) > w then
  67.                 -- Print a multiline word              
  68.                 while string.len( text ) > 0 do
  69.                 if x > w then
  70.                     newLine()
  71.                 end
  72.                     term.write( text )
  73.                     text = string.sub( text, (w-x) + 2 )
  74.                     x,y = term.getCursorPos()
  75.                 end
  76.             else
  77.                 -- Print a word normally
  78.                 if x + string.len(text) > w then
  79.                     newLine()
  80.                 end
  81.                 term.write( text )
  82.                 x,y = term.getCursorPos()
  83.             end
  84.         end
  85.     end
  86.    
  87.     return nLinesPrinted
  88. end
  89.  
  90. function print( ... )
  91.     local nLinesPrinted = 0
  92.     for n,v in ipairs( { ... } ) do
  93.         nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  94.     end
  95.     nLinesPrinted = nLinesPrinted + write( "\n" )
  96.     return nLinesPrinted
  97. end
  98.  
  99. function read()
  100.     term.setCursorBlink(true)
  101.     local str = ""
  102.     local x, y = term.getCursorPos()
  103.     local pos = 1
  104.     local w, h = term.getSize()
  105.    
  106.     local function redraw()
  107.         term.setCursorPos(x, y)
  108.         term.write( string.rep(" ", w - x + 1) )
  109.         term.setCursorPos(x, y)
  110.         term.write(str)
  111.         term.setCursorPos(x + pos - 1, y)
  112.     end
  113.    
  114.     while true do
  115.         local e, p1, p2, p3, p4 = os.pullEvent()
  116.         if e == "key" then --Key       
  117.             if p1 == 205 then --Right
  118.                 if pos <= str:len() then
  119.                     pos = pos + 1
  120.                 end
  121.                 redraw()
  122.             elseif p1 == 203 then --Left
  123.                 if pos > 1 then
  124.                     pos = pos - 1
  125.                 end
  126.                 redraw()
  127.             elseif p1 == 28 then --Enter
  128.                 write("\n")
  129.                 break
  130.             elseif p1 == 14 then --Backspace
  131.                 if pos > 1 then
  132.                     pos = pos - 1
  133.                     str = str:sub(1, pos - 1)..str:sub(pos + 1)
  134.                     redraw()
  135.                 end
  136.             end
  137.         elseif e == "char" then --Character
  138.             str = str:sub(1, pos - 1)..p1..str:sub(pos)
  139.             pos = pos + 1
  140.             redraw()
  141.         end
  142.         redraw()
  143.     end
  144.    
  145.     return str
  146. end
  147.  
  148. --[[
  149. function os.seekFile(name)
  150.     local path = "/"
  151.    
  152.     while running do
  153.         local files = fs.list(path)
  154.         for _, file in pairs(files) do
  155.             if file == name  and not fs.isDir(path..name) then
  156.                 return path..file
  157.             end
  158.         end
  159.        
  160.         for _, file in pairs(files) do
  161.             if fs.isDir(path..file) then
  162.                 files = fs.list(path..file)
  163.                 path = path..file.."/"
  164.             end
  165.         end
  166.     end
  167. end
  168. ]]
  169.  
  170. function os.remove(path)
  171.     fs.delete(path)
  172. end
  173.  
  174. loadfile = function( _sFile )
  175.     local file = fs.open( _sFile, "r" )
  176.     if file then
  177.         local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  178.         file.close()
  179.         return func, err
  180.     end
  181.     return nil, "File not found"
  182. end
  183.  
  184. dofile = function( _sFile )
  185.     local fnFile, e = loadfile( _sFile )
  186.     if fnFile then
  187.         setfenv( fnFile, getfenv(2) )
  188.         fnFile()
  189.     else
  190.         error( e )
  191.     end
  192. end
  193.  
  194. -- Install the rest of the OS api
  195. function os.run( _tEnv, _sPath, ... )
  196.     local tArgs = { ... }
  197.     local fnFile, err = loadfile( _sPath )
  198.     if fnFile then
  199.         local tEnv = _tEnv
  200.         setmetatable( tEnv, { __index = _G } )
  201.         setfenv( fnFile, tEnv )
  202.         local ok, err = pcall( function()
  203.             fnFile( unpack( tArgs ) )
  204.         end )
  205.         if not ok then
  206.             if err and err ~= "" then
  207.                 print( err )
  208.             end
  209.             return false
  210.         end
  211.         return true
  212.     end
  213.     if err and err ~= "" then
  214.         print( err )
  215.     end
  216.     return false
  217. end
  218.  
  219. local nativegetmetatable = getmetatable
  220. function getmetatable( _t )
  221.     if type( _t ) == "string" then
  222.         error( "Attempt to access string metatable" )
  223.     end
  224.     return nativegetmetatable( _t )
  225. end
  226.  
  227. local bProtected = true
  228. local function protect( _t )
  229.     local meta = getmetatable( _t )
  230.     if meta == "Protected" then
  231.         -- already protected
  232.         return
  233.     end
  234.    
  235.     setmetatable( _t, {
  236.         __newindex = function( t, k, v )
  237.             if bProtected then
  238.                 error( "Attempt to write to global" )
  239.             else
  240.                 rawset( t, k, v )
  241.             end
  242.         end,
  243.         __metatable = "Protected",
  244.     } )
  245. end
  246.  
  247. local tAPIsLoading = {}
  248. function os.loadAPI( _sPath )
  249.     local sName = fs.getName( _sPath )
  250.     if tAPIsLoading[sName] == true then
  251.         print( "API "..sName.." is already being loaded" )
  252.         return false
  253.     end
  254.     tAPIsLoading[sName] = true
  255.        
  256.     local tEnv = {}
  257.     setmetatable( tEnv, { __index = _G } )
  258.     local fnAPI, err = loadfile( _sPath )
  259.     if fnAPI then
  260.         setfenv( fnAPI, tEnv )
  261.         fnAPI()
  262.     else
  263.         print( err )
  264.         return false
  265.     end
  266.    
  267.     local tAPI = {}
  268.     for k,v in pairs( tEnv ) do
  269.         tAPI[k] =  v
  270.     end
  271.     protect( tAPI )
  272.    
  273.     bProtected = false
  274.     _G[sName] = tAPI
  275.     bProtected = true
  276.    
  277.     tAPIsLoading[sName] = nil
  278.     return true
  279. end
  280.  
  281. function os.unloadAPI( _sName )
  282.     if _sName ~= "_G" and type(_G[_sName]) == "table" then
  283.         bProtected = false
  284.         _G[_sName] = nil
  285.         bProtected = true
  286.     end
  287. end
  288.  
  289. function os.sleep( _nTime )
  290.     sleep( _nTime )
  291. end
  292.  
  293. function os.remove(path)
  294.     if fs.exists(path) then
  295.         fs.delete(path)
  296.     else
  297.         print(path..": No such file or directory")
  298.     end
  299. end
  300.  
  301. local nativeShutdown = os.shutdown
  302. function os.shutdown()
  303.     nativeShutdown()
  304.     while true do
  305.         coroutine.yield()
  306.     end
  307. end
  308.  
  309. -- Install the lua part of the HTTP api (if enabled)
  310. if http then
  311.     local function wrapRequest( _url, _post )
  312.         local requestID = http.request( _url, _post )
  313.         while true do
  314.             local event, param1, param2 = os.pullEvent()
  315.             if event == "http_success" and param1 == _url then
  316.                 return param2
  317.             elseif event == "http_failure" and param1 == _url then
  318.                 return nil
  319.             end
  320.         end    
  321.     end
  322.    
  323.     http.get = function( _url )
  324.         return wrapRequest( _url, nil )
  325.     end
  326.  
  327.     http.post = function( _url, _post )
  328.         return wrapRequest( _url, _post or "" )
  329.     end
  330. end
  331.  
  332. -- Install the lua part of the peripheral api
  333. peripheral.wrap = function( _sSide )
  334.     if peripheral.isPresent( _sSide ) then
  335.         local tMethods = peripheral.getMethods( _sSide )
  336.         local tResult = {}
  337.         for n,sMethod in ipairs( tMethods ) do
  338.             tResult[sMethod] = function( ... )
  339.                 return peripheral.call( _sSide, sMethod, ... )
  340.             end
  341.         end
  342.         return tResult
  343.     end
  344.     return nil
  345. end
  346.  
  347. -- Protect the global table against modifications
  348. protect( _G )
  349. for k,v in pairs( _G ) do
  350.     if type(v) == "table" then
  351.         protect( v )
  352.     end
  353. end
  354.  
  355. -- Load APIs
  356. local tApis = fs.list( "rom/apis" )
  357. for n,sFile in ipairs( tApis ) do
  358.     if string.sub( sFile, 1, 1 ) ~= "." then
  359.         local sPath = fs.combine( "rom/apis", sFile )
  360.         if not fs.isDir( sPath ) then
  361.             os.loadAPI( sPath )
  362.         end
  363.     end
  364. end
  365.  
  366. if turtle then
  367.     local tApis = fs.list( "rom/apis/turtle" )
  368.     for n,sFile in ipairs( tApis ) do
  369.         if string.sub( sFile, 1, 1 ) ~= "." then
  370.             local sPath = fs.combine( "rom/apis/turtle", sFile )
  371.             if not fs.isDir( sPath ) then
  372.                 os.loadAPI( sPath )
  373.             end
  374.         end
  375.     end
  376. end
  377.  
  378. -- Run the shell
  379. local ok, err = pcall( function()
  380.     parallel.waitForAny(
  381.         function()
  382.             rednet.run()
  383.         end,
  384.         function()
  385.             os.run( {}, "rom/programs/shell" )
  386.         end
  387.     )
  388. end )
  389.  
  390. -- If the shell errored, let the user read it.
  391. if not ok then
  392.     print( err )
  393. end
  394.  
  395. pcall( function()
  396.     term.setCursorBlink( false )
  397.     print( "Press any key to continue" )
  398.     os.pullEvent( "key" )
  399. end )
  400. os.shutdown()
Add Comment
Please, Sign In to add comment