Created
August 11, 2015 02:36
-
-
Save operator-DD3/6d302976e88df4c407ca to your computer and use it in GitHub Desktop.
A fake operating system created in Lua.
This file contains 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
FOS = {} | |
-- serialize functions | |
local function char(c) return ("\\%3d"):format(c:byte()) end | |
local function szstr(s) return ('("%s")'):format(s:gsub("[^ !#-~]", char)) end | |
local function szfun(f) return "loadstring"..szstr(string.dump(f)) end | |
function hex_dump(buf) for i=1,math.ceil(#buf/16) * 16 do if (i-1) % 16 == 0 then io.write(string.format('%08X ', i-1)) end io.write( i > #buf and ' ' or string.format('%02X ', buf:byte(i)) ) if i % 8 == 0 then io.write(' ') end if i % 16 == 0 then io.write( buf:sub(i-16+1, i):gsub('%c','.'), '\n' ) end end end | |
-- Root directory | |
FOS["/"] = {"bin", "home"} | |
-- System Binaries | |
FOS["/bin"] = {"beep", "cat", "cd", "clear", "date", "echo", "ls", "lua", "ping", "pwd", "readline", "time", "version"} | |
FOS["/bin/ls"] = function(s) return table.concat(FOS[FOS.curdir]," ") end | |
FOS["/bin/pwd"] = function() return FOS.curdir end | |
FOS["/bin/cd"] = function(s) FOS.curdir = s end | |
FOS["/bin/date"] = function(s) return os.date("%x") end | |
FOS["/bin/time"] = function() return os.date("%X") end | |
--FOS["/bin/cat"] = function(o) if type(FOS[o]) == "function" then return "Permission Denied" elseif type(FOS[o])== "table" then return "Folder:"..o elseif FOS[o] == nil then return "error: File Inexistant" else return tostring(FOS[o]) end end | |
FOS["/bin/cat"] = function(o) if type(FOS[o]) == "function" then return hex_dump(tostring(FOS[o])) elseif type(FOS[o])== "table" then return "Folder:"..o elseif FOS[o] == nil then return "error: File Inexistant" else return tostring(FOS[o]) end end | |
FOS["/bin/echo"] = function(s) s = "" for i = 1,#FOS.args do s = s .. FOS.args[i] .. " " end return s end | |
FOS["/bin/readline"] = function() return io.read() end | |
FOS["/bin/version"] = function() return "FalseOS v" .. FOS.version .. " 2015" end | |
FOS["/bin/clear"] = function() os.execute("clear") end | |
--FOS["/bin/clear"] = function() for i = 1,20 do print("") end end | |
FOS["/bin/sh"] = function(s) os.execute("sh" .. s) end | |
FOS["/bin/lua"] = function() os.execute("luajit") end | |
FOS["/bin/ping"] = function(s) s = "" for i = 1,#FOS.args do s = s .. FOS.args[i] .. " " end os.execute("ping" .. s) end | |
FOS["/bin/login"] = function() print(FOS.logo) io.write("Login:"); FOS.user = io.read() end | |
FOS["/bin/beep"] = function(s) os.execute("beep" .. s) end | |
-- Home directory | |
FOS["/home"] = {"count", "README.txt"} | |
FOS["/home/count"] = function() for i = 1, 10 do io.write(i .. " ") end end | |
FOS["/home/README.txt"] = [[Welcome Home! | |
This is where you can store your files.]] | |
-- FOS variables | |
FOS.logo = [[ | |
________ _ ___ _______ _____________________________________ | |
\ ____/| \\ \\ ____\\ ________________________________/ | |
\ \___ | \\ \\___ \ \ \_____ ...................... | |
\ _/ | \ \\ \ _\ \ \ ___/ ~~~~ 0.13 Alpha ~~~~ | |
\ \ | |\ \\ \/____\_ \ \______ ~~~~ ~~~~ | |
\_\ |_| \_\\________/ \_______/ ~~~~ skvmb ~~~~ | |
`````````````````````` | |
]] | |
FOS.version = "0.13a" -- ls,pwd,cd,date,time,cat,echo,interactive WIP,readline,version,clear,sh,ping | |
FOS.curdir = "/" | |
FOS.prompt = function() return FOS.user .. "@" .. FOS.curdir .. ": " end | |
FOS.args = {} | |
print("Booting to FalseOS.") | |
-- Interactive Mode local FOS.user = '' | |
FOS["/bin/login"]() | |
-- display prompt | |
local kmnd = "" | |
io.write("\n" .. FOS.prompt()) | |
kmnd = FOS["/bin/readline"]() | |
while(kmnd ~= "exit") do | |
-- splt command into words | |
for word in kmnd:gmatch("%S+") do | |
FOS.args[#FOS.args + 1] = word | |
-- DEBUG *********** -- | |
--print("Arg1="..FOS.args[1]) | |
--print(FOS.args[#FOS.args]) | |
end | |
-- trim command from args | |
local command = FOS.args[1] | |
FOS.args[1]="" | |
local args = table.concat(FOS.args) | |
-- if 1st word is in /bin then execute with next words as args | |
if FOS["/bin/" .. command] ~= nil then | |
--print("Command:"..command) | |
print(FOS["/bin/" .. command](args) ) | |
else | |
-- if 1st word is in curdir then execute etc. | |
if FOS[FOS.curdir .. command] ~= nil then | |
print(FOS[FOS.curdir .. command](args)) | |
else | |
-- else display error | |
print("error: " .. command) | |
end | |
end | |
-- display prompt | |
FOS.args = nil | |
FOS.args = {} | |
io.write("\n" .. FOS.prompt()) | |
kmnd = FOS["/bin/readline"]() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment