Advertisement
Guest User

init.lua

a guest
Aug 31st, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | None | 0 0
  1. do
  2.   _G._OSVERSION = "OpenOS 1.2"
  3.  
  4.   local component = component
  5.   local computer = computer
  6.   local unicode = unicode
  7.  
  8.   -- Low level dofile implementation to read filesystem libraries.
  9.   local rom = {}
  10.   function rom.invoke(method, ...)
  11.     return component.invoke(computer.getBootAddress(), method, ...)
  12.   end
  13.   function rom.open(file) return rom.invoke("open", file) end
  14.   function rom.read(handle) return rom.invoke("read", handle, math.huge) end
  15.   function rom.close(handle) return rom.invoke("close", handle) end
  16.   function rom.inits() return ipairs(rom.invoke("list", "boot")) end
  17.   function rom.isDirectory(path) return rom.invoke("isDirectory", path) end
  18.  
  19.   local screen = component.list('screen')()
  20.   for address in component.list('screen') do
  21.     if #component.invoke(address, 'getKeyboards') > 0 then
  22.       screen = address
  23.     end
  24.   end
  25.  
  26.   -- Report boot progress if possible.
  27.   local gpu = component.list("gpu")()
  28.   local w, h
  29.  
  30.   if gpu and screen then
  31.     component.invoke(gpu, "bind", screen)
  32. --    w, h = component.invoke(gpu, "getResolution")
  33.     w, h = 160, 44 --Override default resolution for custom screen settings
  34.     component.invoke(gpu, "setResolution", w, h)
  35.     component.invoke(gpu, "setBackground", 0x000000)
  36.     component.invoke(gpu, "setForeground", 0xFFFFFF)
  37.     component.invoke(gpu, "fill", 1, 1, w, h, " ")
  38.   end
  39.  
  40.  
  41.   local y = 1
  42.   local function status(msg)
  43.     if gpu and screen then
  44.       component.invoke(gpu, "set", 1, y, msg)
  45.       if y == h then
  46.         component.invoke(gpu, "copy", 1, 2, w, h - 1, 0, -1)
  47.         component.invoke(gpu, "fill", 1, h, w, 1, " ")
  48.       else
  49.         y = y + 1
  50.       end
  51.     end
  52.   end
  53.  
  54.   status("Booting " .. _OSVERSION .. "...")
  55.  
  56.   -- Custom low-level loadfile/dofile implementation reading from our ROM.
  57.   local function loadfile(file)
  58.     status("> " .. file)
  59.     local handle, reason = rom.open(file)
  60.     if not handle then
  61.       error(reason)
  62.     end
  63.     local buffer = ""
  64.     repeat
  65.       local data, reason = rom.read(handle)
  66.       if not data and reason then
  67.         error(reason)
  68.       end
  69.       buffer = buffer .. (data or "")
  70.     until not data
  71.     rom.close(handle)
  72.     return load(buffer, "=" .. file)
  73.   end
  74.  
  75.   local function dofile(file)
  76.     local program, reason = loadfile(file)
  77.     if program then
  78.       local result = table.pack(pcall(program))
  79.       if result[1] then
  80.         return table.unpack(result, 2, result.n)
  81.       else
  82.         error(result[2])
  83.       end
  84.     else
  85.       error(reason)
  86.     end
  87.   end
  88.  
  89.   status("Initializing package management...")
  90.  
  91.   -- Load file system related libraries we need to load other stuff moree
  92.   -- comfortably. This is basically wrapper stuff for the file streams
  93.   -- provided by the filesystem components.
  94.   local package = dofile("/lib/package.lua")
  95.  
  96.   do
  97.     -- Unclutter global namespace now that we have the package module.
  98.     _G.component = nil
  99.     _G.computer = nil
  100.     _G.process = nil
  101.     _G.unicode = nil
  102.  
  103.     -- Initialize the package module with some of our own APIs.
  104.     package.preload["buffer"] = loadfile("/lib/buffer.lua")
  105.     package.preload["component"] = function() return component end
  106.     package.preload["computer"] = function() return computer end
  107.     package.preload["filesystem"] = loadfile("/lib/filesystem.lua")
  108.     package.preload["io"] = loadfile("/lib/io.lua")
  109.     package.preload["unicode"] = function() return unicode end
  110.  
  111.     -- Inject the package and io modules into the global namespace, as in Lua.
  112.     _G.package = package
  113.     _G.io = require("io")
  114.   end
  115.  
  116.   status("Initializing file system...")
  117.  
  118.   -- Mount the ROM and temporary file systems to allow working on the file
  119.   -- system module from this point on.
  120.   local filesystem = require("filesystem")
  121.   filesystem.mount(computer.getBootAddress(), "/")
  122.  
  123.   status("Running boot scripts...")
  124.  
  125.   -- Run library startup scripts. These mostly initialize event handlers.
  126.   local scripts = {}
  127.   for _, file in rom.inits() do
  128.     local path = "boot/" .. file
  129.     if not rom.isDirectory(path) then
  130.       table.insert(scripts, path)
  131.     end
  132.   end
  133.   table.sort(scripts)
  134.   for i = 1, #scripts do
  135.     dofile(scripts[i])
  136.   end
  137.  
  138.   -- Initialize process module.
  139.   require("process").install("/init.lua", "init")
  140.  
  141.   status("Initializing components...")
  142.  
  143.   for c, t in component.list() do
  144.     computer.pushSignal("component_added", c, t)
  145.   end
  146.   os.sleep(0.5) -- Allow signal processing by libraries.
  147.   computer.pushSignal("init") -- so libs know components are initialized.
  148.  
  149.   status("Starting shell...")
  150. end
  151.  
  152. local function motd()
  153.   local f = io.open("/etc/motd")
  154.   if not f then
  155.     return
  156.   end
  157.   if f:read(2) == "#!" then
  158.     f:close()
  159.     os.execute("/etc/motd")
  160.   else
  161.     f:seek("set", 0)
  162.     print(f:read("*a"))
  163.     f:close()
  164.   end
  165. end
  166.  
  167. os.execute("resolution 80 25")
  168.  
  169. while true do
  170.   require("term").clear()
  171.   motd()
  172.   local result, reason = os.execute(os.getenv("SHELL"))
  173.   if not result then
  174.     io.stderr:write((tostring(reason) or "unknown error") .. "\n")
  175.     print("Press any key to continue.")
  176.     os.sleep(0.5)
  177.     require("event").pull("key")
  178.   end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement