Advertisement
Redxone

[CubeSpace]DOS - By Redxone

Jul 22nd, 2015
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.42 KB | None | 0 0
  1. --]] CC-DOS Interface By: Redxone [[--
  2. -- IGN: trainerred2000            
  3. -- BIOS: MultMine
  4. -- Project Leader: Gonow32 :)
  5.  
  6.  
  7. local _osver = "CS-DOS v3.5"
  8. local w, h = term.getSize()
  9.  
  10. _DOS = {
  11.     runQue = "",
  12.     reqFiles = {"batedit"};
  13.     setpos = term.setCursorPos,
  14.     setback = term.setBackgroundColor,
  15.     settext = term.setTextColor,
  16.     curtext = colors.white,
  17.     curback = colors.black,
  18.     clear = function() term.clear() term.setCursorPos(1,1) end;
  19.     inDir = "",
  20.     input_history = {};
  21.     isCmd = false,
  22.     run = true,
  23.     getScreenSize = term.getSize,
  24.     runProg = shell.run,
  25.     echo = true,
  26.  
  27.     start = function(self,file)
  28.         --]] open file THEN load each string as if it where a command [[--
  29.             f = io.open(shell.resolve(file),"r")
  30.             local errline = 1
  31.             for line in f:lines() do
  32.  
  33.                 dcmd = line
  34.             if(dcmd:sub(1,2) ~= "::" and (dcmd ~= "" or dcmd ~= " ") )then
  35.                 local ok = false
  36.  
  37.             for i = 1, #self.cmds do
  38.                  if(dcmd:sub(1,#self.cmds[i].name) == self.cmds[i].name)then
  39.                     self.cmds[i].run(self,dcmd:sub(#self.cmds[i].name+2,-1),true)
  40.                     errline = errline + 1
  41.                     ok=true
  42.                  end
  43.             end
  44.             else
  45.                 ok = true
  46.             end
  47.  
  48.             if not ok then
  49.                 print(file..":ln"..errline.." unrecognized command : "..dcmd)
  50.                 return
  51.             end
  52.         end
  53.     end,
  54.  
  55.  
  56.     tcol = function(self,code)
  57.         local bcol = {
  58.  
  59.             ["0"] = colors.black,
  60.             ["1"] = colors.blue,
  61.             ["2"] = colors.green,
  62.             ["3"] = colors.cyan,
  63.             ["4"] = colors.red,
  64.             ["5"] = colors.purple,
  65.             ["6"] = colors.brown,
  66.             ["7"] = colors.white,
  67.             ["8"] = colors.gray,
  68.             ["9"] = colors.lightBlue,
  69.             ["A"] = colors.lime,
  70.             ["B"] = colors.lightBlue,
  71.             ["C"] = colors.pink,
  72.             ["D"] = colors.pink,
  73.             ["E"] = colors.yellow,
  74.             ["F"] = colors.white,
  75.         }
  76.  
  77.         if(bcol[code] == nil)then
  78.             return colors.white
  79.         else
  80.             return bcol[code]
  81.         end
  82.    
  83.     end,
  84.    
  85.     getfiles = function(self,dir)
  86.         self.settext(colors.white)
  87.         dir=dir.."/"
  88.         local dirs = {}
  89.         local files = {}
  90.         local tBytes = 0
  91.         print("Directory of :"..dir..".")
  92.         for k, v in pairs(fs.list(dir)) do
  93.             if(fs.isDir(dir..v))then table.insert(dirs,v)
  94.             else table.insert(files,v) tBytes = tBytes + fs.getSize(dir..v) end
  95.         end
  96.         textutils.tabulate(colors.orange, dirs, colors.white, files)
  97.         print("   "..#files.." File(s)              "..tBytes.." Bytes.")
  98.         print("   "..#dirs.." Dirs(s)          "..fs.getFreeSpace(dir).." Bytes Free.")
  99.         self.settext(self.curtext)
  100.     end,   
  101.  
  102.  
  103.     cmds = {
  104.  
  105.         {name="cd",desc="Change directory",run=function(self,v)
  106.             if(v=="" or v==" ")then print("The syntax of the command is incorrect. \n") return end
  107.  
  108.                     if(fs.exists(shell.resolve(v)))then
  109.                         shell.setDir(shell.resolve(v))
  110.                     else
  111.                         self.settext(colors.red)
  112.                         print("Unknown directory: "..shell.resolve(v).."\n")
  113.                         self.settext(colors.white)
  114.                     end
  115.         end};
  116.         {name="@echo",desc="",run=function(self,v)
  117.             if(self.echo)then isecho = "on" else isecho = "off" end
  118.                 if(v == "" or v == " ")then
  119.                      print("echo is "..isecho)
  120.                 elseif(v == "off")then
  121.                     self.echo = false
  122.                 elseif(v == "on")then
  123.                     self.echo = true
  124.                 else
  125.                     print(v)
  126.                 end
  127.         end};
  128.         {name="copy",desc="Copy files to a destination",run=function(self,v)
  129.             if(v == "" or v == " ")then
  130.                 print("The syntax of the command is incorrect. \n")
  131.                 return
  132.             end
  133.             local args = {}
  134.  
  135.             for i in string.gmatch(v, "%S+") do
  136.                 if(#args < 2)then table.insert(args,i) end
  137.             end
  138.  
  139.             if(#args < 2)then print("The syntax of the command is incorrect. \n") return end
  140.  
  141.             if(fs.exists(shell.resolve(args[1])))then
  142.                 self.runProg('copy',v)
  143.             else
  144.                 print("The system could not find the file specified. \n")
  145.                 return
  146.             end
  147.         end};
  148.         {name="move",desc="Move files to a destination",run=function(self,v)
  149.             if(v == "" or v == " ")then
  150.                 print("The syntax of the command is incorrect. \n")
  151.                 return
  152.             end
  153.             local args = {}
  154.  
  155.             for i in string.gmatch(v, "%S+") do
  156.                 if(#args < 2)then table.insert(args,i) end
  157.             end
  158.  
  159.             if(#args < 2)then print("The syntax of the command is incorrect. \n") return end
  160.  
  161.             if(fs.exists(shell.resolve(args[1])))then
  162.                 self.runProg('move',v)
  163.             else
  164.                 print("The system could not find the file specified. \n")
  165.                 return
  166.             end
  167.         end};
  168.         {name="rename",desc="Rename files",run=function(self,v)
  169.             if(v == "" or v == " ")then
  170.                 print("The syntax of the command is incorrect. \n")
  171.                 return
  172.             end
  173.             local args = {}
  174.  
  175.             for i in string.gmatch(v, "%S+") do
  176.                 if(#args < 2)then table.insert(args,i) end
  177.             end
  178.  
  179.             if(#args < 2)then print("The syntax of the command is incorrect. \n") return end
  180.  
  181.             if(fs.exists(shell.resolve(args[1])))then
  182.                 self.runProg('move',v)
  183.             else
  184.                 print("The system could not find the file specified. \n")
  185.                 return
  186.             end
  187.         end};
  188.         {name= "mkdir",desc="Make a directory",run=function(self,v)
  189.             if(not fs.exists(shell.resolve(v)) )then
  190.                 fs.makeDir(shell.resolve(v))
  191.             else
  192.                 print("Directory or file exists. \n")
  193.             end
  194.             end};
  195.         {name= "del",desc="Delete a file or directory",run=function(self,v)
  196.             if( fs.exists(shell.resolve(v)) )then
  197.                 fs.delete(shell.resolve(v))
  198.             else
  199.                 print("Directory or file doesnt exists. \n")
  200.             end
  201.             end};
  202.         {name="color",desc="Sets console color",run=function(self,v)
  203.             if(#v < 2)then print("The syntax of the command is incorrect. \n") return end
  204.                 self.setback(self.tcol(self,v:sub(2,2)))
  205.                 self.settext(self.tcol(self,v:sub(1,1)))
  206.                 self.curtext = self.tcol(self,v:sub(1,1))
  207.                 self.curback = self.tcol(self,v:sub(2,2))
  208.          end};
  209.         {name= "echo",desc="Displays messages",run=function(self,v,isf)
  210.             if(v == "on")then if(isf)then write(">echo on \n") end
  211.             self.echo = true elseif(v == "off")then if(isf)then write(">echo off \n") end
  212.             self.echo = false
  213.             else
  214.                 print(v)
  215.             end
  216.         end};
  217.         {name="shutdown",desc="Shutdown the system",run=function(self,v) os.shutdown() end};
  218.         {name="reboot",desc="Reboot the system",run=function(self,v) os.reboot() end};
  219.         {name="time",desc="Displays system time",run=function(self,v) print("The current time is "..textutils.formatTime(os.time(),false).."\n") end};
  220.         --{name="reload",desc="Reload CS-DOS",run=function(self,v) self.run=false self.runProg(shell.getRunningProgram()) return end};
  221.         {name="exit",desc="Exits shell",run = function(self, v) self.run = false return end};
  222.         --{name="error",desc="Test error message",run = function(self, v) error("Exception Caught!") end};
  223.         {name="cls",desc="Clear screen",run = function(self, v) self.clear() end};
  224.         {name="edit",desc="Edit a file",run = function(self, v)
  225.             if(v=="" or v==" ")then
  226.                 print("The syntax of the command is incorrect. \n")
  227.                 return
  228.             end
  229.                 local ed = ""
  230.                 for i in v:gmatch("([^.]+)") do
  231.                     ed=i
  232.                 end
  233.                 if(ed == "bat")then
  234.                     self.runProg("batedit",v)
  235.                     self.settext(colors.white)
  236.                     self.setback(colors.black)
  237.                 else
  238.                     self.runProg("edit",v)
  239.                 end
  240.  
  241.             end};
  242.         {name="start",desc="",run=function(self,v)
  243.  
  244.                 if(fs.exists(shell.resolve(v)))then
  245.             local ed = ""
  246.                 for i in v:gmatch("([^.]+)") do
  247.                     ed=i
  248.                 end
  249.                 if(ed == "bat")then
  250.                     self:start(v)
  251.                 else
  252.                     shell.run(shell.resolve(v))
  253.                 end
  254.         else
  255.             print("System could not find the file specified: ".._inp..". \n")
  256.         end
  257.  
  258.         end};
  259.         {name="dir",desc="Lists files and directorys",run = function(self, v)
  260.             if( fs.exists( shell.resolve(v) ) )then
  261.                 self.getfiles(self,shell.resolve(v))
  262.             else
  263.                 print("System could not find the directory specified. \n")
  264.             end
  265.         end};
  266.         {name="help",desc="Lists all commands",run = function(self, v)
  267.             write("CS-DOS Commands: \n")
  268.             for i = 1, #self.cmds do
  269.                 if(self.cmds[i].desc ~= "")then
  270.                     write(string.upper(self.cmds[i].name)..string.rep(" ",8 - #self.cmds[i].name))
  271.                     local w, h = self.getScreenSize()
  272.                     write(" ")
  273.                     write(self.cmds[i].desc.."\n")
  274.                 end
  275.             end
  276.          end};
  277.     };
  278.    
  279. }
  280.  
  281. --]] this will make teh program EXTRA LONG [[ --
  282. -- Get file to edit
  283.  
  284.  
  285. -- Cleanup
  286. term.clear()
  287. term.setCursorBlink( false )
  288. term.setCursorPos( 1, 1 )
  289. -- end bat editor
  290. local _logo = {
  291.    
  292.     " -----  ----    ----    ---  ----  ",
  293.     " |      |       |   |  |   | |     ",
  294.     " |      |       |   |  |   | |     ",
  295.     " |      ----    |   |  |   | ----  ",
  296.     " |          |   |   |  |   |     | ",
  297.     " |          |   |   |  |   |     | ",
  298.     " ----   ----    ----    ---  ----  ",
  299. }
  300.  
  301. _DOS.clear()
  302. -- functions
  303.  
  304. local _printlogo = function()
  305.     _DOS.setback(colors.blue)
  306.     for i = 1, #_logo do
  307.         _DOS.settext(colors.green)
  308.         print(_logo[i])
  309.         _DOS.settext(colors.white)
  310.     end
  311.  
  312.     print(string.rep(" ",_logo[1]:len()))
  313.     print(_osver,string.rep(" ", (_logo[1]:len() - #_osver )))
  314.    
  315.     _DOS.setback(colors.black)
  316. end
  317.  
  318. -- Manage
  319. _system = function()
  320.  
  321.     if(_DOS.echo)then
  322.         write(shell.dir().."> ")
  323.     end
  324.  
  325.     _inp = read(nil,_DOS.input_history)
  326.     table.insert(_DOS.input_history,_inp)
  327.    
  328.     _DOS.isCmd = false
  329.     _inp = string.lower(_inp)
  330.     -- Catch Potential Error --
  331.     _proInp = _inp
  332.     -- check command
  333.  
  334.     for i = 1, #_DOS.cmds do
  335.         _proInp = _inp:sub(1,#_DOS.cmds[i].name)
  336.         for i in string.gmatch(_proInp, "%S+") do
  337.             nProInp = i
  338.         end
  339.         if(_proInp == nProInp)then nProInp = "" end
  340.         if(_proInp == _DOS.cmds[i].name and nProInp ~= _proInp)then
  341.             _DOS.cmds[i].run(_DOS,_inp:sub(#_DOS.cmds[i].name+2,-1))
  342.             _DOS.isCmd = true
  343.         end
  344.     end
  345.  
  346.     if(not _DOS.isCmd)then
  347.         if(fs.exists(shell.resolve(_inp)))then
  348.             local ed = ""
  349.                 for i in _inp:gmatch("([^.]+)") do
  350.                     ed=i
  351.                 end
  352.                 if(ed == "bat")then
  353.                     _DOS:start(_inp)
  354.                 else
  355.                     shell.run(_inp)
  356.                 end
  357.         else
  358.             print("Bad command or file name: ".._inp..". \n")
  359.         end
  360.     end
  361. end
  362.  
  363.  
  364. -- Commands
  365.  
  366.  
  367. -- Loop
  368. _OS = function()
  369.     if(not fs.exists("autoexec.bat"))then
  370.         print("system could not find the file autoexec.bat!")
  371.         print("creating...")
  372.         f = fs.open("autoexec.bat","w")
  373.         f.writeLine("::Autoexec generated by CS-DOS::")
  374.         f.writeLine("echo Welcome To CS-DOS! ")
  375.         f.writeLine("time")
  376.         f.close()
  377.     end
  378.  
  379.     _DOS:start("autoexec.bat")
  380.  
  381.     print("Starting ".._osver)
  382.     while _DOS.run do
  383.         if(_DOS.runQue ~= "")then
  384.             _DOS.start(_DOS,_DOS.runQue)
  385.             _DOS.runQue = ""
  386.         end
  387.         for i = 1, #_DOS.reqFiles do if(not fs.exists(_DOS.reqFiles[i]) ) then
  388.                 error("Required File Not Found: ".._DOS.reqFiles[i])
  389.             end
  390.         end
  391.  
  392.         local _gud, _value = pcall(_system)
  393.  
  394.         if(not _gud)then
  395.             print("ERROR SEVERE: ".._value..". \n")
  396.         end
  397.  
  398.     end
  399. end
  400.  
  401. _OS()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement