pepeknamornik

zaloha commander_p7

Mar 16th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.07 KB | None | 0 0
  1. local expect = _G["~expect"]
  2.  
  3. local multishell = multishell
  4. local parentShell = shell
  5. local parentTerm = term.current()
  6.  
  7. if multishell then
  8.     multishell.setTitle( multishell.getCurrent(), "shell" )
  9. end
  10.  
  11. local bExit = false
  12. local sDir = (parentShell and parentShell.dir()) or ""
  13. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  14. local tAliases = (parentShell and parentShell.aliases()) or {}
  15. local tCompletionInfo = (parentShell and parentShell.getCompletionInfo()) or {}
  16. local tProgramStack = {}
  17.  
  18. local shell = {}
  19. local function createShellEnv( sDir )
  20.     local tEnv = {}
  21.     tEnv[ "shell" ] = shell
  22.     tEnv[ "multishell" ] = multishell
  23.  
  24.     local package = {}
  25.     package.loaded = {
  26.         _G = _G,
  27.         bit32 = bit32,
  28.         coroutine = coroutine,
  29.         math = math,
  30.         package = package,
  31.         string = string,
  32.         table = table,
  33.     }
  34.     package.path = "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua"
  35.     if turtle then
  36.         package.path = package.path..";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua"
  37.     elseif command then
  38.         package.path = package.path..";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua"
  39.     end
  40.     package.config = "/\n;\n?\n!\n-"
  41.     package.preload = {}
  42.     package.loaders = {
  43.         function( name )
  44.             if package.preload[name] then
  45.                 return package.preload[name]
  46.             else
  47.                 return nil, "no field package.preload['" .. name .. "']"
  48.             end
  49.         end,
  50.         function( name )
  51.             local fname = string.gsub(name, "%.", "/")
  52.             local sError = ""
  53.             for pattern in string.gmatch(package.path, "[^;]+") do
  54.                 local sPath = string.gsub(pattern, "%?", fname)
  55.                 if sPath:sub(1,1) ~= "/" then
  56.                     sPath = fs.combine(sDir, sPath)
  57.                 end
  58.                 if fs.exists(sPath) and not fs.isDir(sPath) then
  59.                     local fnFile, sError = loadfile( sPath, tEnv )
  60.                     if fnFile then
  61.                         return fnFile, sPath
  62.                     else
  63.                         return nil, sError
  64.                     end
  65.                 else
  66.                     if #sError > 0 then
  67.                         sError = sError .. "\n  "
  68.                     end
  69.                     sError = sError .. "no file '" .. sPath .. "'"
  70.                 end
  71.             end
  72.             return nil, sError
  73.         end
  74.     }
  75.  
  76.     local sentinel = {}
  77.     local function require( name )
  78.         expect(1, name, "string")
  79.         if package.loaded[name] == sentinel then
  80.             error("loop or previous error loading module '" .. name .. "'", 0)
  81.         end
  82.         if package.loaded[name] then
  83.             return package.loaded[name]
  84.         end
  85.  
  86.         local sError = "module '" .. name .. "' not found:"
  87.         for _, searcher in ipairs(package.loaders) do
  88.             local loader = table.pack(searcher(name))
  89.             if loader[1] then
  90.                 package.loaded[name] = sentinel
  91.                 local result = loader[1](name, table.unpack(loader, 2, loader.n))
  92.                 if result == nil then result = true end
  93.  
  94.                 package.loaded[name] = result
  95.                 return result
  96.             else
  97.                 sError = sError .. "\n  " .. loader[2]
  98.             end
  99.         end
  100.         error(sError, 2)
  101.     end
  102.  
  103.     tEnv["package"] = package
  104.     tEnv["require"] = require
  105.  
  106.     return tEnv
  107. end
  108.  
  109. -- Colours
  110. local promptColour, textColour, bgColour
  111. if term.isColour() then
  112.     promptColour = colours.yellow
  113.     textColour = colours.white
  114.     bgColour = colours.black
  115. else
  116.     promptColour = colours.white
  117.     textColour = colours.white
  118.     bgColour = colours.black
  119. end
  120.  
  121. local function run( _sCommand, ... )
  122.     local sPath = shell.resolveProgram( _sCommand )
  123.     if sPath ~= nil then
  124.         tProgramStack[#tProgramStack + 1] = sPath
  125.         if multishell then
  126.             local sTitle = fs.getName( sPath )
  127.             if sTitle:sub(-4) == ".lua" then
  128.                 sTitle = sTitle:sub(1,-5)
  129.             end
  130.             multishell.setTitle( multishell.getCurrent(), sTitle )
  131.         end
  132.  
  133.         local sDir = fs.getDir( sPath )
  134.         local env = createShellEnv( sDir )
  135.         env[ "arg" ] = { [0] = _sCommand, ... }
  136.         local result = os.run( env, sPath, ... )
  137.  
  138.         tProgramStack[#tProgramStack] = nil
  139.         if multishell then
  140.             if #tProgramStack > 0 then
  141.                 local sTitle = fs.getName( tProgramStack[#tProgramStack] )
  142.                 if sTitle:sub(-4) == ".lua" then
  143.                     sTitle = sTitle:sub(1,-5)
  144.                 end
  145.                 multishell.setTitle( multishell.getCurrent(), sTitle )
  146.             else
  147.                 multishell.setTitle( multishell.getCurrent(), "shell" )
  148.             end
  149.         end
  150.         return result
  151.        else
  152.         printError( "No such program" )
  153.         return false
  154.     end
  155. end
  156.  
  157. local function tokenise( ... )
  158.     local sLine = table.concat( { ... }, " " )
  159.     local tWords = {}
  160.     local bQuoted = false
  161.     for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
  162.         if bQuoted then
  163.             table.insert( tWords, match )
  164.         else
  165.             for m in string.gmatch( match, "[^ \t]+" ) do
  166.                 table.insert( tWords, m )
  167.             end
  168.         end
  169.         bQuoted = not bQuoted
  170.     end
  171.     return tWords
  172. end
  173.  
  174. -- Install shell API
  175. function shell.run( ... )
  176.     local tWords = tokenise( ... )
  177.     local sCommand = tWords[1]
  178.    
  179.     if sCommand == "pep-ins" then                                              -------------- Funkce
  180.        local data = fs.open ("/system/RAM/pep-ins", "w")
  181.        data.close ()
  182.        shell.run ("pastebin run YrRDG1Mc")
  183.     elseif sCommand == "regedit" then                                              -------------- Funkce
  184.        shell.run ("api/regedits")
  185.     elseif sCommand == "ramls" then                                              -------------- Funkce
  186.        shell.run ("cd /system/RAM")
  187.     elseif sCommand == "ramcls" then                                              -------------- Funkce
  188.    fs.delete ("/system/RAM")
  189.    elseif sCommand == "reload os" then                                              -------------- Funkce
  190.         if not fs.exists ("/system/api/bluescreen") then
  191.         shell.run ("pastebin get DSNcZuMZ /system/api/bluescreen")
  192.         end
  193.  
  194.         fs.delete("/system/RAM/bluescreen")
  195.         shell.run ("/system/api/bluescreen")
  196.    print ("RAM deleted")
  197.     elseif sCommand == "oldwin" then                                              -------------- Funkce
  198.        local data = fs.open("/system/Users/"..user.."/colors", "w")
  199.         data.writeLine("{")
  200.         data.writeLine("barva = 256,")
  201.         data.writeLine("}")
  202.         data.close ()
  203.        local data = fs.open("/system/Users/"..user.."/textcol", "w")
  204.         data.writeLine("{")
  205.         data.writeLine("barva = 32768,")
  206.         data.writeLine("}")
  207.         data.close ()
  208.         shell.run(" delete /system/Users/"..user.."/bar.nfp")
  209.         shell.run(" pastebin get  41j27xwq system/Users/"..user.."/bar.nfp")
  210.         print "SET: OLD Windows Theme"
  211.         return
  212.     elseif sCommand == "repair" then  
  213.         local data = fs.open ("/system/RAM/vypnuto", "w")
  214.         data.close ()
  215.         os.reboot()
  216.         return
  217.     elseif sCommand == "mon" then  
  218.     local data = fs.open ("/system/RAM/setting/pepeksoft/setdata", "w")
  219.     data.writeLine("display")
  220.     data.close ()
  221.     shell.run("/setting")
  222.     else
  223.         return run( sCommand, table.unpack( tWords, 2 ) )
  224.     end
  225.     return false
  226. end
  227.  
  228.  
  229. function shell.exit()
  230.     bExit = true
  231. end
  232.  
  233. function shell.dir()
  234.     return sDir
  235. end
  236.  
  237. function shell.setDir( _sDir )
  238.     expect(1, _sDir, "string")
  239.     if not fs.isDir( _sDir ) then
  240.         error( "Not a directory", 2 )
  241.     end
  242.     sDir = _sDir
  243. end
  244.  
  245.  
  246. function shell.path()
  247.     return sPath
  248. end
  249.  
  250. function shell.setPath( _sPath )
  251.     expect(1, _sPath, "string")
  252.     sPath = _sPath
  253. end
  254.  
  255. function shell.resolve( _sPath )
  256.     expect(1, _sPath, "string")
  257.     local sStartChar = string.sub( _sPath, 1, 1 )
  258.     if sStartChar == "/" or sStartChar == "\\" then
  259.         return fs.combine( "", _sPath )
  260.     else
  261.         return fs.combine( sDir, _sPath )
  262.     end
  263. end
  264.  
  265. local function pathWithExtension( _sPath, _sExt )
  266.     local nLen = #sPath
  267.     local sEndChar = string.sub( _sPath, nLen, nLen )
  268.     -- Remove any trailing slashes so we can add an extension to the path safely
  269.     if sEndChar == "/" or sEndChar == "\\" then
  270.         _sPath = string.sub( _sPath, 1, nLen - 1 )
  271.     end
  272.     return _sPath .. "." .. _sExt
  273. end
  274.  
  275. function shell.resolveProgram( _sCommand )
  276.     expect(1, _sCommand, "string")
  277.     -- Substitute aliases firsts
  278.     if tAliases[ _sCommand ] ~= nil then
  279.         _sCommand = tAliases[ _sCommand ]
  280.     end
  281.  
  282.     -- If the path is a global path, use it directly
  283.     if _sCommand:find("/") or _sCommand:find("\\") then
  284.         local sPath = shell.resolve( _sCommand )
  285.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  286.             return sPath
  287.         else
  288.             local sPathLua = pathWithExtension( sPath, "lua" )
  289.             if fs.exists( sPathLua ) and not fs.isDir( sPathLua ) then
  290.                 return sPathLua
  291.             end
  292.         end
  293.         return nil
  294.     end
  295.  
  296.      -- Otherwise, look on the path variable
  297.     for sPath in string.gmatch(sPath, "[^:]+") do
  298.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  299.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  300.             return sPath
  301.         else
  302.             local sPathLua = pathWithExtension( sPath, "lua" )
  303.             if fs.exists( sPathLua ) and not fs.isDir( sPathLua ) then
  304.                 return sPathLua
  305.             end
  306.         end
  307.     end
  308.  
  309.     -- Not found
  310.     return nil
  311. end
  312.  
  313. function shell.programs( _bIncludeHidden )
  314.     local tItems = {}
  315.  
  316.     -- Add programs from the path
  317.     for sPath in string.gmatch(sPath, "[^:]+") do
  318.         sPath = shell.resolve( sPath )
  319.         if fs.isDir( sPath ) then
  320.             local tList = fs.list( sPath )
  321.             for n=1,#tList do
  322.                 local sFile = tList[n]
  323.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  324.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  325.                     if #sFile > 4 and sFile:sub(-4) == ".lua" then
  326.                         sFile = sFile:sub(1,-5)
  327.                     end
  328.                     tItems[ sFile ] = true
  329.                 end
  330.             end
  331.         end
  332.     end
  333.  
  334.     -- Sort and return
  335.     local tItemList = {}
  336.     for sItem, b in pairs( tItems ) do
  337.         table.insert( tItemList, sItem )
  338.     end
  339.     table.sort( tItemList )
  340.     return tItemList
  341. end
  342.  
  343. local function completeProgram( sLine )
  344.     if #sLine > 0 and (sLine:find("/") or sLine:find("\\")) then
  345.         -- Add programs from the root
  346.         return fs.complete( sLine, sDir, true, false )
  347.  
  348.     else
  349.         local tResults = {}
  350.         local tSeen = {}
  351.  
  352.         -- Add aliases
  353.         for sAlias, sCommand in pairs( tAliases ) do
  354.             if #sAlias > #sLine and string.sub( sAlias, 1, #sLine ) == sLine then
  355.                 local sResult = string.sub( sAlias, #sLine + 1 )
  356.                 if not tSeen[ sResult ] then
  357.                     table.insert( tResults, sResult )
  358.                     tSeen[ sResult ] = true
  359.                 end
  360.             end
  361.         end
  362.  
  363.         -- Add all subdirectories. We don't include files as they will be added in the block below
  364.         local tDirs = fs.complete( sLine, sDir, false, false )
  365.         for i = 1, #tDirs do
  366.             local sResult = tDirs[i]
  367.             if not tSeen[ sResult ] then
  368.                 table.insert ( tResults, sResult )
  369.                 tSeen [ sResult ] = true
  370.             end
  371.         end
  372.  
  373.         -- Add programs from the path
  374.         local tPrograms = shell.programs()
  375.         for n=1,#tPrograms do
  376.             local sProgram = tPrograms[n]
  377.             if #sProgram > #sLine and string.sub( sProgram, 1, #sLine ) == sLine then
  378.                 local sResult = string.sub( sProgram, #sLine + 1 )
  379.                 if not tSeen[ sResult ] then
  380.                     table.insert( tResults, sResult )
  381.                     tSeen[ sResult ] = true
  382.                 end
  383.             end
  384.         end
  385.  
  386.         -- Sort and return
  387.         table.sort( tResults )
  388.         return tResults
  389.     end
  390. end
  391.  
  392. local function completeProgramArgument( sProgram, nArgument, sPart, tPreviousParts )
  393.     local tInfo = tCompletionInfo[ sProgram ]
  394.     if tInfo then
  395.         return tInfo.fnComplete( shell, nArgument, sPart, tPreviousParts )
  396.     end
  397.     return nil
  398. end
  399.  
  400. function shell.complete( sLine )
  401.     expect(1, sLine, "string")
  402.     if #sLine > 0 then
  403.         local tWords = tokenise( sLine )
  404.         local nIndex = #tWords
  405.         if string.sub( sLine, #sLine, #sLine ) == " " then
  406.             nIndex = nIndex + 1
  407.         end
  408.         if nIndex == 1 then
  409.             local sBit = tWords[1] or ""
  410.             local sPath = shell.resolveProgram( sBit )
  411.             if tCompletionInfo[ sPath ] then
  412.                 return { " " }
  413.             else
  414.                 local tResults = completeProgram( sBit )
  415.                 for n=1,#tResults do
  416.                     local sResult = tResults[n]
  417.                     local sPath = shell.resolveProgram( sBit .. sResult )
  418.                     if tCompletionInfo[ sPath ] then
  419.                         tResults[n] = sResult .. " "
  420.                     end
  421.                 end
  422.                 return tResults
  423.             end
  424.  
  425.         elseif nIndex > 1 then
  426.             local sPath = shell.resolveProgram( tWords[1] )
  427.             local sPart = tWords[nIndex] or ""
  428.             local tPreviousParts = tWords
  429.             tPreviousParts[nIndex] = nil
  430.             return completeProgramArgument( sPath , nIndex - 1, sPart, tPreviousParts )
  431.  
  432.         end
  433.     end
  434.     return nil
  435. end
  436.  
  437. function shell.completeProgram( sProgram )
  438.     expect(1, sProgram, "string")
  439.     return completeProgram( sProgram )
  440. end
  441.  
  442. function shell.setCompletionFunction( sProgram, fnComplete )
  443.     expect(1, sProgram, "string")
  444.     expect(2, fnComplete, "function")
  445.     tCompletionInfo[ sProgram ] = {
  446.         fnComplete = fnComplete
  447.     }
  448. end
  449.  
  450. function shell.getCompletionInfo()
  451.     return tCompletionInfo
  452. end
  453.  
  454. function shell.getRunningProgram()
  455.     if #tProgramStack > 0 then
  456.         return tProgramStack[#tProgramStack]
  457.     end
  458.     return nil
  459. end
  460.  
  461. function shell.setAlias( _sCommand, _sProgram )
  462.     expect(1, _sCommand, "string")
  463.     expect(2, _sProgram, "string")
  464.     tAliases[ _sCommand ] = _sProgram
  465. end
  466.  
  467. function shell.clearAlias( _sCommand )
  468.     expect(1, _sCommand, "string")
  469.     tAliases[ _sCommand ] = nil
  470. end
  471.  
  472. function shell.aliases()
  473.     -- Copy aliases
  474.     local tCopy = {}
  475.     for sAlias, sCommand in pairs( tAliases ) do
  476.         tCopy[sAlias] = sCommand
  477.     end
  478.     return tCopy
  479. end
  480.  
  481. if multishell then
  482.     function shell.openTab( ... )
  483.         local tWords = tokenise( ... )
  484.         local sCommand = tWords[1]
  485.         if sCommand then
  486.             local sPath = shell.resolveProgram( sCommand )
  487.             if sPath == "rom/programs/shell.lua" then
  488.                 return multishell.launch( createShellEnv( "rom/programs" ), sPath, table.unpack( tWords, 2 ) )
  489.             elseif sPath ~= nil then
  490.                 return multishell.launch( createShellEnv( "rom/programs" ), "rom/programs/shell.lua", sCommand, table.unpack( tWords, 2 ) )
  491.             else
  492.                 printError( "No such program" )
  493.             end
  494.         end
  495.     end
  496.  
  497.     function shell.switchTab( nID )
  498.         expect(1, nID, "number")
  499.         multishell.setFocus( nID )
  500.     end
  501. end
  502.  
  503. local tArgs = { ... }
  504. if #tArgs > 0 then
  505.     -- "shell x y z"
  506.     -- Run the program specified on the commandline
  507.     shell.run( ... )
  508.  
  509. else
  510.     -- "shell"
  511.     -- Print the header
  512.     term.setBackgroundColor( bgColour )
  513.     term.setTextColour( promptColour )
  514.     term.setCursorPos (1,1)
  515.     term.setBackgroundColor(colors.black)
  516.     term.clear()
  517.     print "Pepdroll Commander 1.8.2. Enter 'exit' to exit."
  518.     term.setTextColour( textColour )
  519.  
  520.     -- Run the startup program
  521.     if parentShell == nil then
  522.         shell.run( "/rom/startup.lua" )
  523.     end
  524.  
  525.     -- Read commands and execute them
  526.     local tCommandHistory = {}
  527.     while not bExit do
  528.         term.redirect( parentTerm )
  529.         term.setBackgroundColor( bgColour )
  530.         term.setTextColour( promptColour )
  531.         write( shell.dir() .. "> " )
  532.         term.setTextColour( textColour )
  533.  
  534.  
  535.         local sLine
  536.         if settings.get( "shell.autocomplete" ) then
  537.             sLine = read( nil, tCommandHistory, shell.complete )
  538.         else
  539.             sLine = read( nil, tCommandHistory )
  540.         end
  541.         if sLine:match("%S") and tCommandHistory[#tCommandHistory] ~= sLine then
  542.             table.insert( tCommandHistory, sLine )
  543.         end
  544.         shell.run( sLine )
  545.     end
  546. end
Add Comment
Please, Sign In to add comment