Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Project info:
- Name: uOS
- Creator: Jesusthekiller
- Language: Lua (CC)
- Website: None
- License: GNU GPL
- License file can be fount at www.jesusthekiller.com/license-gpl.html
- Version: 1.0
- ]]--
- --[[
- Changelog:
- 1.0:
- Initial Release
- ]]--
- --[[
- LICENSE:
- uOS
- Copyright (c) 2013 Jesusthekiller
- This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- ]]--
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- -- COPIED (edited a bit) FROM CRAFTOS SHELL:
- local bExit = false
- local sDir = ""
- local sPath = ".:/rom/programs"
- local tAliases = {}
- local tProgramStack = {}
- local shell = {}
- local tEnv = {
- ["shell"] = shell,
- }
- local function run( _sCommand, ... )
- local sPath = shell.resolveProgram( _sCommand )
- if sPath ~= nil then
- tProgramStack[#tProgramStack + 1] = sPath
- local result = os.run( tEnv, sPath, ... )
- tProgramStack[#tProgramStack] = nil
- return result
- else
- printError( "No such program" )
- return false
- end
- end
- local function runLine( _sLine )
- local tWords = {}
- for match in string.gmatch( _sLine, "[^ \t]+" ) do
- table.insert( tWords, match )
- end
- local sCommand = tWords[1]
- if sCommand then
- return run( sCommand, unpack( tWords, 2 ) )
- end
- return false
- end
- function shell.run( ... )
- return runLine( table.concat( { ... }, " " ) )
- end
- function shell.exit()
- os.shutdown()
- end
- function shell.dir()
- return sDir
- end
- function shell.setDir( _sDir )
- sDir = _sDir
- end
- function shell.path()
- return sPath
- end
- function shell.setPath( _sPath )
- sPath = _sPath
- end
- function shell.resolve( _sPath )
- local sStartChar = string.sub( _sPath, 1, 1 )
- if sStartChar == "/" or sStartChar == "\\" then
- return fs.combine( "", _sPath )
- else
- return fs.combine( sDir, _sPath )
- end
- end
- function shell.resolveProgram( _sCommand )
- -- Substitute aliases firsts
- if tAliases[ _sCommand ] ~= nil then
- _sCommand = tAliases[ _sCommand ]
- end
- -- If the path is a global path, use it directly
- local sStartChar = string.sub( _sCommand, 1, 1 )
- if sStartChar == "/" or sStartChar == "\\" then
- local sPath = fs.combine( "", _sCommand )
- if fs.exists( sPath ) and not fs.isDir( sPath ) then
- return sPath
- end
- return nil
- end
- -- Otherwise, look on the path variable
- for sPath in string.gmatch(sPath, "[^:]+") do
- sPath = fs.combine( shell.resolve( sPath ), _sCommand )
- if fs.exists( sPath ) and not fs.isDir( sPath ) then
- return sPath
- end
- end
- -- Not found
- return nil
- end
- function shell.programs( _bIncludeHidden )
- local tItems = {}
- -- Add programs from the path
- for sPath in string.gmatch(sPath, "[^:]+") do
- sPath = shell.resolve( sPath )
- if fs.isDir( sPath ) then
- local tList = fs.list( sPath )
- for n,sFile in pairs( tList ) do
- if not fs.isDir( fs.combine( sPath, sFile ) ) and
- (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
- tItems[ sFile ] = true
- end
- end
- end
- end
- -- Sort and return
- local tItemList = {}
- for sItem, b in pairs( tItems ) do
- table.insert( tItemList, sItem )
- end
- table.sort( tItemList )
- return tItemList
- end
- function shell.getRunningProgram()
- if #tProgramStack > 0 then
- return tProgramStack[#tProgramStack]
- end
- return nil
- end
- function shell.setAlias( _sCommand, _sProgram )
- tAliases[ _sCommand ] = _sProgram
- end
- function shell.clearAlias( _sCommand )
- tAliases[ _sCommand ] = nil
- end
- function shell.aliases()
- -- Add aliases
- local tCopy = {}
- for sAlias, sCommand in pairs( tAliases ) do
- tCopy[sAlias] = sCommand
- end
- return tCopy
- end
- -- END OF COPY
- -- ######################################################################################################
- print("LuaOS Successfully booted!")
- print("Lua 5.2\n")
- if fs.exists("_startup") then
- shell.run("_startup")
- end
- local tCmds = {}
- while true do
- write("> ")
- local cmd = read(nil, tCmds)
- table.insert(tCmds, cmd)
- cmd = (cmd == "help") and ".help" or cmd
- if cmd:sub(1,1) == "." then
- --[[
- if cmd:lower() == ".reboot" then
- os.reboot()
- elseif cmd:lower() == ".shutdown" then
- os.shutdown()
- elseif cmd:lower():sub(1,5) == ".help" then
- print("To execute files (as in CraftOS): .<cmd> <args>")
- print("Everything else is run as Lua code")
- else
- shell.run(cmd:sub(2))
- end]]--
- cmd = cmd:sub(2)
- if cmd:sub(1, 4) == "help" then
- print("Execute command: .<cmd> <args>")
- print(" Commands:")
- print(" edit <file>")
- print(" rm <file>")
- print(" cp <from> <to>")
- print(" ls")
- print(" run <file> <args>")
- print(" pastebin <mode> <code> <name>")
- print(" [clear|cls]")
- print(" exit")
- print(" reboot")
- print(" help")
- print("Everything else is run as Lua code")
- elseif cmd:sub(1, 4) == "edit" then
- shell.run(cmd)
- elseif cmd:sub(1, 3) == "run" then
- shell.run(cmd:sub(5))
- elseif cmd:sub(1, 8) == "pastebin" then
- shell.run("/rom/programs/http/"..cmd)
- elseif cmd:sub(1, 4) == "exit" then
- os.shutdown()
- elseif cmd:sub(1, 6) == "reboot" then
- os.reboot()
- elseif cmd:sub(1,2) == "rm" then
- shell.run("/rom/programs/delete"..cmd:sub(3))
- elseif cmd:sub(1,2) == "cp" then
- shell.run("/rom/programs/copy"..cmd:sub(3))
- elseif cmd:sub(1,2) == "ls" then
- shell.run("/rom/programs/list")
- elseif cmd:sub(1,5) == "clear" or cmd:sub(1,3) == "cls" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- else
- print("Unknown command!")
- end
- else
- local s = cmd
- -- COPIED FROM "LUA"
- local nForcePrint = 0
- local func, e = loadstring( s, "lua" )
- local func2, e2 = loadstring( "return "..s, "lua" )
- if not func then
- if func2 then
- func = func2
- e = nil
- nForcePrint = 1
- end
- else
- if func2 then
- func = func2
- end
- end
- if func then
- -- setfenv( func, tEnv )
- local tResults = { pcall( function() return func() end ) }
- if tResults[1] then
- local n = 1
- while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
- print( tostring( tResults[n + 1] ) )
- n = n + 1
- end
- else
- printError( tResults[2] )
- end
- else
- printError( e )
- end
- -- END OF COPY
- end
- end
- shell.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement