Advertisement
jesusthekiller

uOS Term

Jun 16th, 2013
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: uOS
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 1.0
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.       1.0:
  17.         Initial Release
  18. ]]--
  19.  
  20. --[[
  21.     LICENSE:
  22.    
  23.     uOS
  24.     Copyright (c) 2013 Jesusthekiller
  25.  
  26.     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.
  27.  
  28.     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.
  29.  
  30.     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/>.
  31. ]]--
  32.  
  33. term.setBackgroundColor(colors.black)
  34. term.setTextColor(colors.white)
  35. term.clear()
  36. term.setCursorPos(1, 1)
  37.  
  38. -- COPIED (edited a bit) FROM CRAFTOS SHELL:
  39. local bExit = false
  40. local sDir = ""
  41. local sPath = ".:/rom/programs"
  42. local tAliases = {}
  43. local tProgramStack = {}
  44.  
  45. local shell = {}
  46. local tEnv = {
  47.     ["shell"] = shell,
  48. }
  49.  
  50. local function run( _sCommand, ... )
  51.     local sPath = shell.resolveProgram( _sCommand )
  52.     if sPath ~= nil then
  53.         tProgramStack[#tProgramStack + 1] = sPath
  54.         local result = os.run( tEnv, sPath, ... )
  55.         tProgramStack[#tProgramStack] = nil
  56.         return result
  57.     else
  58.         printError( "No such program" )
  59.         return false
  60.     end
  61. end
  62.  
  63. local function runLine( _sLine )
  64.     local tWords = {}
  65.     for match in string.gmatch( _sLine, "[^ \t]+" ) do
  66.         table.insert( tWords, match )
  67.     end
  68.  
  69.     local sCommand = tWords[1]
  70.     if sCommand then
  71.         return run( sCommand, unpack( tWords, 2 ) )
  72.     end
  73.     return false
  74. end
  75.  
  76. function shell.run( ... )
  77.     return runLine( table.concat( { ... }, " " ) )
  78. end
  79.  
  80. function shell.exit()
  81.     os.shutdown()
  82. end
  83.  
  84. function shell.dir()
  85.     return sDir
  86. end
  87.  
  88. function shell.setDir( _sDir )
  89.     sDir = _sDir
  90. end
  91.  
  92. function shell.path()
  93.     return sPath
  94. end
  95.  
  96. function shell.setPath( _sPath )
  97.     sPath = _sPath
  98. end
  99.  
  100. function shell.resolve( _sPath )
  101.     local sStartChar = string.sub( _sPath, 1, 1 )
  102.     if sStartChar == "/" or sStartChar == "\\" then
  103.         return fs.combine( "", _sPath )
  104.     else
  105.         return fs.combine( sDir, _sPath )
  106.     end
  107. end
  108.  
  109. function shell.resolveProgram( _sCommand )
  110.     -- Substitute aliases firsts
  111.     if tAliases[ _sCommand ] ~= nil then
  112.         _sCommand = tAliases[ _sCommand ]
  113.     end
  114.  
  115.     -- If the path is a global path, use it directly
  116.     local sStartChar = string.sub( _sCommand, 1, 1 )
  117.     if sStartChar == "/" or sStartChar == "\\" then
  118.         local sPath = fs.combine( "", _sCommand )
  119.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  120.             return sPath
  121.         end
  122.         return nil
  123.     end
  124.    
  125.     -- Otherwise, look on the path variable
  126.     for sPath in string.gmatch(sPath, "[^:]+") do
  127.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  128.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  129.             return sPath
  130.         end
  131.     end
  132.    
  133.     -- Not found
  134.     return nil
  135. end
  136.  
  137. function shell.programs( _bIncludeHidden )
  138.     local tItems = {}
  139.    
  140.     -- Add programs from the path
  141.     for sPath in string.gmatch(sPath, "[^:]+") do
  142.         sPath = shell.resolve( sPath )
  143.         if fs.isDir( sPath ) then
  144.             local tList = fs.list( sPath )
  145.             for n,sFile in pairs( tList ) do
  146.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  147.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  148.                     tItems[ sFile ] = true
  149.                 end
  150.             end
  151.         end
  152.     end
  153.  
  154.     -- Sort and return
  155.     local tItemList = {}
  156.     for sItem, b in pairs( tItems ) do
  157.         table.insert( tItemList, sItem )
  158.     end
  159.     table.sort( tItemList )
  160.     return tItemList
  161. end
  162.  
  163. function shell.getRunningProgram()
  164.     if #tProgramStack > 0 then
  165.         return tProgramStack[#tProgramStack]
  166.     end
  167.     return nil
  168. end
  169.  
  170. function shell.setAlias( _sCommand, _sProgram )
  171.     tAliases[ _sCommand ] = _sProgram
  172. end
  173.  
  174. function shell.clearAlias( _sCommand )
  175.     tAliases[ _sCommand ] = nil
  176. end
  177.  
  178. function shell.aliases()
  179.     -- Add aliases
  180.     local tCopy = {}
  181.     for sAlias, sCommand in pairs( tAliases ) do
  182.         tCopy[sAlias] = sCommand
  183.     end
  184.     return tCopy
  185. end
  186. -- END OF COPY
  187.  
  188. -- ######################################################################################################
  189.  
  190. print("LuaOS Successfully booted!")
  191. print("Lua 5.2\n")
  192.  
  193. if fs.exists("_startup") then
  194.     shell.run("_startup")
  195. end
  196.  
  197. local tCmds = {}
  198.  
  199. while true do
  200.     write("> ")
  201.     local cmd = read(nil, tCmds)
  202.    
  203.     table.insert(tCmds, cmd)
  204.    
  205.     cmd = (cmd == "help") and ".help" or cmd
  206.    
  207.     if cmd:sub(1,1) == "." then
  208.         --[[
  209.         if cmd:lower() == ".reboot" then
  210.             os.reboot()
  211.         elseif cmd:lower() == ".shutdown" then
  212.             os.shutdown()
  213.         elseif cmd:lower():sub(1,5) == ".help" then
  214.             print("To execute files (as in CraftOS): .<cmd> <args>")
  215.             print("Everything else is run as Lua code")
  216.         else
  217.             shell.run(cmd:sub(2))
  218.         end]]--
  219.        
  220.         cmd = cmd:sub(2)
  221.        
  222.         if cmd:sub(1, 4) == "help" then
  223.             print("Execute command: .<cmd> <args>")
  224.             print("  Commands:")
  225.             print("    edit <file>")
  226.             print("    rm <file>")
  227.             print("    cp <from> <to>")
  228.             print("    ls")
  229.             print("    run <file> <args>")
  230.             print("    pastebin <mode> <code> <name>")
  231.             print("    [clear|cls]")
  232.             print("    exit")
  233.             print("    reboot")
  234.             print("    help")
  235.             print("Everything else is run as Lua code")
  236.         elseif cmd:sub(1, 4) == "edit" then
  237.             shell.run(cmd)
  238.         elseif cmd:sub(1, 3) == "run" then
  239.             shell.run(cmd:sub(5))
  240.         elseif cmd:sub(1, 8) == "pastebin" then
  241.             shell.run("/rom/programs/http/"..cmd)
  242.         elseif cmd:sub(1, 4) == "exit" then
  243.             os.shutdown()
  244.         elseif cmd:sub(1, 6) == "reboot" then
  245.             os.reboot()
  246.         elseif cmd:sub(1,2) == "rm" then
  247.             shell.run("/rom/programs/delete"..cmd:sub(3))
  248.         elseif cmd:sub(1,2) == "cp" then
  249.             shell.run("/rom/programs/copy"..cmd:sub(3))
  250.         elseif cmd:sub(1,2) == "ls" then
  251.             shell.run("/rom/programs/list")
  252.         elseif cmd:sub(1,5) == "clear" or cmd:sub(1,3) == "cls" then
  253.             term.setBackgroundColor(colors.black)
  254.             term.setTextColor(colors.white)
  255.             term.clear()
  256.             term.setCursorPos(1, 1)
  257.         else
  258.             print("Unknown command!")
  259.         end
  260.     else
  261.         local s = cmd
  262.         -- COPIED FROM "LUA"
  263.         local nForcePrint = 0
  264.         local func, e = loadstring( s, "lua" )
  265.         local func2, e2 = loadstring( "return "..s, "lua" )
  266.         if not func then
  267.             if func2 then
  268.                 func = func2
  269.                 e = nil
  270.                 nForcePrint = 1
  271.             end
  272.         else
  273.             if func2 then
  274.                 func = func2
  275.             end
  276.         end
  277.        
  278.         if func then
  279.             -- setfenv( func, tEnv )
  280.             local tResults = { pcall( function() return func() end ) }
  281.             if tResults[1] then
  282.                 local n = 1
  283.                 while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
  284.                     print( tostring( tResults[n + 1] ) )
  285.                     n = n + 1
  286.                 end
  287.             else
  288.                 printError( tResults[2] )
  289.             end
  290.         else
  291.             printError( e )
  292.         end
  293.         -- END OF COPY
  294.     end
  295. end
  296.  
  297. shell.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement