boomboomthehacker

skex

Sep 15th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. m=component.proxy(component.list("modem")())
  2. m.open(2412)
  3. computer.beep(2000)
  4. local eventStack = {}
  5. local listeners = {}
  6. event = {}
  7. function event.listen(evtype,callback)
  8.  if listeners[evtype] ~= nil then
  9.   table.insert(listeners[evtype],callback)
  10.   return #listeners
  11.  else
  12.   listeners[evtype] = {callback}
  13.   return 1
  14.  end
  15. end
  16. function event.ignore(evtype,id)
  17.  table.remove(listeners[evtype],id)
  18. end
  19. function event.pull(filter)
  20.  if not filter then return table.remove(eventStack,1)
  21.  else
  22.   for _,v in pairs(eventStack) do
  23.    if v == filter then
  24.     return v
  25.    end
  26.   end
  27.   repeat
  28.    t=table.pack(computer.pullSignal())
  29.    evtype = table.remove(t,1)
  30.    if listeners[evtype] ~= nil then
  31.     for k,v in pairs(listeners[evtype]) do
  32.      local evt,rasin = pcall(v,evtype,table.unpack(t))
  33.      if not evt then
  34.       print("stdout_write","ELF: "..tostring(evtype)..":"..tostring(k)..":"..rasin)
  35.      end
  36.     end
  37.    end
  38.   until evtype == filter
  39.   return evtype, table.unpack(t)
  40.  end
  41. end
  42. function print(...)
  43.  local args=table.pack(...)
  44.  pcall(function() m.broadcast(2412, table.unpack(args)) end)
  45. end
  46. local rS = {}
  47. event.listen("modem_message",function(_,_,_,_,_,cmd)
  48.  table.insert(rS,cmd)
  49.  computer.beep()
  50.  computer.pushSignal("read_return")
  51. end)
  52. function read(pref,repchar)
  53.  local a,_ = table.remove(rS,1)
  54.  if a == nil then
  55.   event.pull("read_return")
  56.   return table.remove(rS,1)
  57.  else return a end
  58. end
  59. local function splitonspace(str)
  60.  local t = {}
  61.  for w in str:gmatch("%S+") do
  62.   table.insert(t,w)
  63.  end
  64.  computer.beep()
  65.  return t
  66. end
  67. function sleep(n)
  68.   local t0 = computer.uptime()
  69.   while computer.uptime() - t0 <= n do computer.pushSignal("wait") computer.pullSignal() end
  70. end
  71. print("skex BIOS v0.1.2")
  72. print((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")
  73. function error(...)
  74.  print(...)
  75. end
  76. local cmds = {"q - quit skex",
  77. "l <start> <end> - writes the lines from <start> (default 1) to <end> (default #b) to stdout",
  78. "lc - writes the current line to stdout",
  79. "s [line] - seeks to a line",
  80. "so - writes the number of the current line to stdout",
  81. "i - inserts/appends lines, a line with only . to exit this mode",
  82. "e - executes the code in the buffer",
  83. "ei - interactive lua mode, works like the OpenOS lua program",
  84. "d - deletes the current line",
  85. "cb - clears the whole buffer",
  86. "r - replaces the current line",
  87. "fb - re-flashes the BIOS from input, . to end and flash the EEPROM",
  88. "? / h - prints this help message"}
  89. local run = true
  90. b = {}
  91. l = 1
  92. while run do
  93.  local cmdt = splitonspace(read())
  94.  if cmdt[1] == "q" then
  95.   run = false
  96.  elseif cmdt[1] == "l" then
  97.   startn,endn = cmdt[2],cmdt[3]
  98.   if cmdt[2] == nil then
  99.    startn=1
  100.   end
  101.   if cmdt[3] == nil then
  102.    endn=#b
  103.   end
  104.   print("Line "..startn.." to "..endn..":")
  105.   for i = tonumber(startn),tonumber(endn) do
  106.    print(b[i])
  107.   end
  108.  elseif cmdt[1] == "lc" then
  109.   print(b[l])
  110.  elseif cmdt[1] == "so" then
  111.   print("Current line: "..l)
  112.  elseif cmdt[1] == "s" then
  113.   l = tonumber(cmdt[2])
  114.  elseif cmdt[1] == "i" then
  115.   c=read()
  116.   repeat
  117.    table.insert(b,l,c)
  118.    l=l+1
  119.    c=read()
  120.   until c=="."
  121.  elseif cmdt[1] == "e" then
  122.   cd=""
  123.   for k,v in pairs(b) do
  124.    cd=cd..v.."\n"
  125.   end
  126.   print(pcall(load(cd)))
  127.  elseif cmdt[1] == "ei" then
  128.   c=read()
  129.   repeat
  130.    print(pcall(load(c)))
  131.    c=read()
  132.   until c=="."
  133.  elseif cmdt[1] == "d" then
  134.   print(table.remove(b,l))
  135.  elseif cmdt[1] == "r" then
  136.   b[l] = read()
  137.  elseif cmdt[1] == "cb" then
  138.   b = {}
  139.  elseif cmdt[1] == "fb" then
  140.   c=read()
  141.   nb = ""
  142.   repeat
  143.    nb = nb .. c .. "\n"
  144.    c = read()
  145.   until c=="."
  146.   eeprom = component.proxy(component.list("eeprom")())
  147.   eeprom.set(nb)
  148.   computer.shutdown(true)
  149.  elseif cmdt[1] == "?" or cmdt[1]:sub(1,1) == "h" then
  150.   print("Commands:")
  151.   print("Arguments surrounded by []s are mandatory, arguments surrounded by <> are optional.")
  152.   for k,v in ipairs(cmds) do
  153.    print(v)
  154.   end
  155.  end
  156. end
Add Comment
Please, Sign In to add comment