Advertisement
shadowkat1010

micrOS init.lua

Sep 25th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. --syscall init
  2. syscall = {}
  3. local syscalls = {}
  4. function syscall.register(name, callback)
  5.  syscalls[name] = callback
  6. end
  7. function syscall.execute(name, ...)
  8.  local call = syscalls[name]
  9.  if call then
  10.   return call(...)
  11.  else
  12.   error(name)
  13.  end
  14. end
  15.  
  16. --screen init
  17. local gpu, screen = component.list("gpu")(), component.list("screen")()
  18. local w, h
  19. if gpu and screen then
  20.  component.invoke(gpu, "bind", screen)
  21.  w, h = component.invoke(gpu, "getResolution")
  22.  component.invoke(gpu, "setResolution", w, h)
  23.  component.invoke(gpu, "setBackground", 0x000000)
  24.  component.invoke(gpu, "setForeground", 0xFFFFFF)
  25.  component.invoke(gpu, "fill", 1, 1, w, h, " ")
  26. end
  27. y = 1
  28. local log = false
  29. --syscall term stuff
  30. syscall.register("stdio_clear",function() local y = 1 component.invoke(gpu, "fill", 1, 1, w, h, " ") end)
  31. syscall.register("stdout_write",function(msg) --register syscall for output
  32.  msg = tostring(msg)
  33.  if gpu and screen then
  34.   component.invoke(gpu, "set", 1, y, msg)
  35.    if y == h then
  36.     component.invoke(gpu, "copy", 1, 2, w, h - 1, 0, -1)
  37.     component.invoke(gpu, "fill", 1, h, w, 1, " ")
  38.    else
  39.     y = y + 1
  40.    end
  41.   end
  42. end)
  43. syscall.register('stdin_read', function(rc)
  44.  local sx, sy = 1, y
  45.  local s = ""
  46.  local gpu = component.proxy(component.list("gpu")())
  47.  local mx, my = gpu.getResolution()
  48.  local function rerender()
  49.   local blanks = ""
  50.   for i = sx+1,mx do
  51.    blanks = blanks.." "
  52.   end
  53.   gpu.set(sx,sy,blanks)
  54.   if rc == nil then
  55.    gpu.set(sx,sy,s.."_")
  56.   else
  57.    local rs = ""
  58.    if s:len() > 0 then
  59.     for i = 1,s:len() do
  60.      rs = rs..rc
  61.     end
  62.    end
  63.    gpu.set(sx,sy,rs.."_")
  64.   end
  65.  end
  66.  rerender()
  67.  repeat
  68.   evtype, _, keya, keyb = computer.pullSignal()
  69.    if evtype == "key_down" then
  70.     if keya > 31 then
  71.      s=s..string.char(keya)
  72.     elseif keya == 8 and keyb == 14 then
  73.      s=s:sub(1,s:len()-1)
  74.     end
  75.    rerender()
  76.   end
  77.  until evtype == "key_down" and keya == 13 and keyb == 28
  78.  if y == my then
  79.   gpu.copy(1, 2, mx, my - 1, 0, -1)
  80.   gpu.fill(1, my, mx, 1, " ")
  81.  else
  82.   y = y + 1
  83.  end
  84.  return s
  85. end)
  86.  
  87. --filesystem code
  88. local fs={}
  89. fs.drive_map={}
  90. fs.activeDrive="A"
  91. syscall.register("fs_drives",function() return fs.drive_map,fs.activeDrive end)
  92. syscall.register("fs_mount",function(proxy,index) fs.drive_map[index]=proxy end)
  93. syscall.execute("fs_mount",component.proxy(computer.getBootAddress()),"A")
  94. syscall.execute("fs_mount",component.proxy(computer.tmpAddress()),"X")
  95. syscall.register("fs_proxy",function(index) return fs.drive_map.index end)
  96. syscall.register("fs_invoke",function(method,...) return fs.drive_map[fs.activeDrive][method](...) end) --wtf?
  97. syscall.register("fs_get_drive",function() return fs.activeDrive end)
  98. syscall.register("fs_set_drive",function(index) fs.activeDrive=index end)
  99. syscall.register("fs_open",function(path,mode) return syscall.execute("fs_invoke","open",path,mode or "r") end)
  100. syscall.register("fs_write",function(handle, data) return syscall.execute("fs_invoke","write",handle,data) end)
  101. syscall.register("fs_read",function(handle, length) return syscall.execute("fs_invoke","read",handle,length or math.huge) end)
  102. syscall.register("fs_close",function(handle) return syscall.execute("fs_invoke","close",handle) end)
  103. syscall.register("fs_exists", function(path) return syscall.execute("fs_invoke","exists",path) end)
  104. syscall.register("fs_is_dir", function(path) return syscall.execute("fs_invoke","isDirectory",path) end)
  105. syscall.register("fs_mkdir", function(path) return syscall.execute("fs_invoke","makeDirectory",path) end)
  106. syscall.register("fs_list", function(path) return syscall.execute("fs_invoke","list",path or "/") end)
  107. syscall.register("fs_remove", function(path) return syscall.execute("fs_invoke","remove",path) end)
  108. syscall.register("fs_move", function(path1, path2) return syscall.execute("fs_invoke","rename", path1, path2) end)
  109.  
  110. --component code -- list, type, proxy, doc, methods
  111. syscall.register("component_list", function(filter) return component.list(filter) end)
  112. syscall.register("component_type", function(addr) return component.type(addr) end)
  113. syscall.register("component_proxy", function(addr) return component.proxy(addr) end)
  114. syscall.register("component_methods", function(addr) return component.methods(addr) end)
  115.  
  116. --computer?
  117. syscall.register("beep",function(freq) computer.beep(freq) end) -- most important part.
  118.  
  119. --derp loop
  120. syscall.execute("fs_mount",component.proxy(computer.getBootAddress()),"A")
  121. syscall.execute("fs_mount",component.proxy(computer.tmpAddress()),"X")
  122. local sout=tostring(math.floor(computer.totalMemory()/1024)).."k total, "..tostring(math.floor(computer.freeMemory()/1024)).."k free, "..tostring(math.floor((computer.totalMemory()-computer.freeMemory())/1024)).."k used"
  123. if syscall.execute("fs_exists","postinit.lua") then
  124.  local f=syscall.execute("fs_open","postinit.lua")
  125.  syscall.execute("stdout_write",tostring(f))
  126.  local c = syscall.execute("fs_read",f)
  127.  syscall.execute("stdout_write",c)
  128.  local functionLoaded = load(c)
  129.  syscall.execute("fs_close",f)
  130.  syscall.execute("stdout_write",tostring(pcall(functionLoaded())))
  131. else
  132.  repeat
  133.   syscall.execute("stdout_write",sout)
  134.   sout=syscall.execute("stdin_read")
  135.   if sout=="load" then
  136.    for k,v in pairs(syscall.execute("fs_list","/")) do
  137.     syscall.execute("stdout_write",tostring(v))
  138.    end
  139.    local f=syscall.execute("fs_open",syscall.execute("stdin_read"))
  140.    syscall.execute("stdout_write",tostring(f))
  141.    local c = syscall.execute("fs_read",f)
  142.    syscall.execute("stdout_write",c)
  143.    local functionLoaded = load(c)
  144.    syscall.execute("fs_close",f)
  145.    syscall.execute("stdout_write",tostring(pcall(functionLoaded())))
  146.   end
  147.  until sout == "kill"
  148. end
  149. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement