Created
February 22, 2020 04:39
-
-
Save Ocawesome101/c5e868019d63b35f1d7d86477ba50a76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua5.2 | |
-- LuaSH -- a basic Linux shell written in Lua. -- | |
local args = {...} | |
local lfs = require("lfs") | |
local PATH = "/bin:/usr/bin:/usr/local/bin:/home/ocawesome101/.local/bin" | |
local exit = false | |
local function log()end | |
if args[1] == "--verbose" then | |
log = function(...)print(...)end | |
end | |
local function split(sep, ...) | |
log("SPLIT", sep) | |
local words = {} | |
local line = table.concat({...}, " ") | |
local sep = sep:sub(1, 1) or " " | |
for word in line:gmatch("([^" .. sep .. "]+)") do | |
log("WORD", word) | |
table.insert(words, word) | |
end | |
log("RETURN") | |
return words | |
end | |
while not exit do | |
io.write("lsh: " .. lfs.currentdir() .. ": " .. os.getenv("USER") .. " ") | |
local command = io.read() | |
if command and command ~= "" then | |
log("EXEC", command) | |
local c = split(" ", command) | |
local p = split(":", PATH) | |
if c[1] == "exit" then | |
log("EXIT") | |
exit = true | |
elseif c[1] == "cd" then | |
if c[2] then | |
lfs.chdir(c[2]) | |
else | |
lfs.chdir(os.getenv("HOME")) | |
end | |
else | |
for _,path in pairs(p) do | |
log("TRYEXEC", path .. "/" .. command) | |
local cmd, err = io.popen(path .. "/" .. command, "r") | |
if not cmd then | |
log("ERR", err) | |
else | |
log("WRITE", table.unpack(c, 2, #c)) | |
repeat | |
log("READ") | |
local out = cmd:read() | |
if out then print(out) end | |
until not out | |
log("CLOSE") | |
cmd:close() | |
break | |
end | |
end | |
end | |
-- local output = os.execute(command) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment