smash 1.66 KB
Newer Older
1 2
-- -*- lua -*-

3
local env, args = ...
4 5 6 7

local interp = function(s, tab)
   return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
8

9 10 11 12 13
if(args[1] == "--help") then
   print("An interactive command shell.\n")
   print("Usage:")
   print("  smash")
else
14 15
   if(io.exists(env.HOME .. "/_smashrc")) then
      assert(loadstring(io.readfile(env.HOME .. "/_smashrc")))(env, args)
16
   else
17 18
      local motd = io.exists("/etc/motd") and io.readfile("/etc/motd") or ""
      print("\n" .. motd .. "\nType \"logout\" to exit and \"ls /bin\"" ..
19 20
               " to see commands available.")
   end
21

22
   while true do
23 24
      set_prompt(interp(env.PROMPT, env))
      local input = orb.expand_globs(interp(io.read(), env), env)
25
      if not input or input == "exit" or input == "logout" then return end
26

27 28
      local var, value = input:match("export (.+)=(.*)")
      local change_dir = input:match("cd +(.+)")
29
      change_dir = change_dir and orb.normalize(change_dir, env.CWD)
30

31 32 33
      -- inlining primitives this way is kinda tacky
      if(input == "cd") then
         env.CWD = env.HOME
34
      elseif(change_dir and not io.exists(change_dir)) then
Phil Hagelberg committed
35
         print(change_dir .. " not found.")
Phil Hagelberg committed
36
      elseif(change_dir and not io.isdir(change_dir)) then
37
         print(change_dir .. " is not a directory")
38
      elseif(change_dir) then
39
         env.CWD = change_dir
40 41 42
      elseif(var) then
         env[var] = value
      elseif(not input:match("^ *$")) then
43
         local success, msg = orb.pexec(env, input, orb.extra_sandbox)
44 45 46 47 48 49
         if(not success) then
            print(msg)
            env.LAST_ERROR = msg
         else
            env.LAST_ERROR = nil
         end
50 51 52
      end
   end
end