Advertisement
Guest User

sh.lua

a guest
Jun 29th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. -- Modified shell to run commands async when you run a command with an '&' at the beginning
  2. -- example: "/home # &file.lua"
  3. local shell = require("shell")
  4. local tty = require("tty")
  5. local text = require("text")
  6. local sh = require("sh")
  7. local thread = require("thread")
  8.  
  9. local args = shell.parse(...)
  10.  
  11. shell.prime()
  12.  
  13. if #args == 0 then
  14.   local has_profile
  15.   local input_handler = {hint = sh.hintHandler}
  16.   while true do
  17.     if io.stdin.tty and io.stdout.tty then
  18.       if not has_profile then -- first time run AND interactive
  19.         has_profile = true
  20.         dofile("/etc/profile.lua")
  21.       end
  22.       if tty.getCursor() > 1 then
  23.         io.write("\n")
  24.       end
  25.       io.write(sh.expand(os.getenv("PS1") or "$ "))
  26.     end
  27.     tty.window.cursor = input_handler
  28.     local command = io.stdin:readLine(false)
  29.     tty.window.cursor = nil
  30.     if command then
  31.       command = text.trim(command)
  32.       if command == "exit" then
  33.         return
  34.       elseif command ~= "" then
  35.         if string.sub(command, 0, 1) == "&" then
  36.           thread.create(function()
  37.             local result, reason = sh.execute(_ENV, string.sub(command, 2))
  38.             if not result then
  39.               io.stderr:write((reason and tostring(reason) or "unknown error") .. "\n")
  40.             end    
  41.           end)
  42.         else
  43.           --luacheck: globals _ENV
  44.           local result, reason = sh.execute(_ENV, command)
  45.           if not result then
  46.             io.stderr:write((reason and tostring(reason) or "unknown error") .. "\n")
  47.           end
  48.         end
  49.       end
  50.     elseif command == nil then -- false only means the input was interrupted
  51.       return -- eof
  52.     end
  53.   end
  54. else
  55.   -- execute command.
  56.   local result = table.pack(sh.execute(...))
  57.   if not result[1] then
  58.     error(result[2], 0)
  59.   end
  60.   return table.unpack(result, 2)
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement