Advertisement
Guest User

ClOS

a guest
Jan 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. -- ClOS Lua adaptation
  2. -- Developed under Macrosaft WinDOS (Microsoft Windows)
  3. -- Will hopefully run on other OSes
  4.  
  5. -- Version 0.0.3
  6.  
  7. -- Created by Clarence Pacelli (clarence112)
  8.  
  9. -- destrubuted under Attribution-NonCommercial-ShareAlike 3.0(CC-BY-NC-SA 3.0)
  10. --    you CAN modify and re-destribute this code, just give credit
  11. --    you CAN'T use this code for commercial purposes, or change the licence
  12.  
  13. --If you came here from the scratch based version of ClOS, welcome!
  14. --greetings to future crew (yes this is nessicary)
  15.  
  16.  
  17.  
  18. --declare dependencies
  19. io = require("io")
  20. os = require("os")
  21.  
  22. -- var setup
  23. restart = 1
  24. osver = "vanilla 0.0.3" --OS version
  25. installedcommands = {}
  26. installeduis = {}
  27. comsuccess = 0
  28.  
  29. --load installed commands
  30. for line in io.lines("installedprograms.txt") do
  31.     table.insert(installedcommands, line)
  32. end
  33.  
  34. --load installed User-Interfaces
  35. for line in io.lines("UIs.txt") do
  36.     table.insert(installeduis, line)
  37. end
  38.  
  39. --main command infastructure
  40. function command(cominput)
  41.  
  42.     comsuccess = 0 --command sucsess check var
  43.  
  44.     compassthrough = cominput
  45.     for i,line in ipairs(installedcommands) do --run any user-installed commands
  46.         if dofile(line) == 1 then
  47.             comsuccess = 1
  48.         end
  49.     end
  50.  
  51.     if cominput[1] == "lala" then --OG test command
  52.         print("'tis a test")
  53.         comsuccess = 1
  54.     end
  55.  
  56.     if cominput[1] == "quit" or cominput[1] == "logout" or cominput[1] == "halt" or cominput[1] == "shutdown" then --exit ClOS
  57.         comsuccess = 1
  58.         print("Goodbye")
  59.         clspgm = 1
  60.         restart = 0
  61.     end
  62.  
  63.     if cominput[1] == "help" then
  64.         comsuccess = 1
  65.         print("choose a catigory (1-3)")
  66.         print(" ")
  67.         print("1: power")
  68.         print("2: easter eggs")
  69.         print("3: utillity")
  70.         io.write("help > ")
  71.         io.flush()
  72.         cominput[1] = io.read()
  73.         if cominput[1] == "1" then
  74.             print("close ClOS:")
  75.             print(" quit")
  76.             print(" logout")
  77.             print(" halt")
  78.             print(" shutdown")
  79.             print("restart ClOS:")
  80.             print(" restart")
  81.         end
  82.         if cominput[1] == "2" then
  83.             print("easter egg commands:")
  84.             print(" lala")
  85.         end
  86.         if cominput[1] == "3" then
  87.             print("Utillity commands:")
  88.             print(" clear........clears the screen")
  89.             print(" fileload.....loads a file")
  90.             print(" fileprint....prints the userdata of the loaded file")
  91.             print(" filerun......runs the loaded file")
  92.             print(" fileunload...unloads the loaded file")
  93.             print(" installed....lists installed programs")
  94.         end
  95.     end
  96.  
  97.     if cominput[1] == "restart" then --restart ClOS
  98.         clspgm = 1
  99.         comsuccess = 1
  100.         print("Restarting")
  101.     end
  102.  
  103.     if cominput[1] == "clear" then --clear the screen
  104.         comsuccess = 1
  105.         os.execute("CLS")
  106.     end
  107.  
  108.     if cominput[1] == "fileload" then --load a file
  109.         comsuccess = 1
  110.         if table.getn(cominput) == 2 then
  111.             loadedfile = io.open(cominput[2])
  112.         elseif table.getn(cominput) == 1 then
  113.             io.write("fileload > ")
  114.             io.flush()
  115.             cominput[2] = io.read()
  116.             loadedfile = io.open(cominput[2])
  117.         end
  118.  
  119.         if (loadedfile) then
  120.             print("Load succsessful")
  121.             loadedfilepath = cominput[2]
  122.         elseif table.getn(cominput) > 2 then
  123.             print("Error: Too many arguments. If the file path has spaces, try running fileload without any arguments.")
  124.         else
  125.             print("File not found")
  126.         end
  127.     end
  128.  
  129.     if cominput[1] == "fileprint" then --print loaded file (userdata)
  130.         if (loadedfile) then
  131.             print(loadedfile)
  132.         else
  133.             print("No file loaded, load one with fileload")
  134.         end
  135.         comsuccess = 1
  136.     end
  137.  
  138.     if cominput[1] == "filerun" then --run loaded file (lua programs only)
  139.         if (loadedfile) then
  140.         local fileext = mysplit(loadedfilepath, ".")
  141.             if fileext[2] == "lua" then
  142.                 installcheck = 1
  143.                 dofile(loadedfilepath)
  144.                 installcheck = 0
  145.             elseif fileext[2] == "clsh" then
  146.                 print("CLSH files do not work right now")
  147.             elseif fileext[2] == "sft" then
  148.                 print("SFT files do not work right now")
  149.             else
  150.                 print("Error: loaded file is not a .lua, .clsh, or .sft file and cannot be run.")
  151.             end
  152.         else
  153.             print("No file loaded, load one with fileload")
  154.         end
  155.         comsuccess = 1
  156.     end
  157.  
  158.     if cominput[1] == "installed" then
  159.         comsuccess = 1
  160.         for i,line in ipairs(installedcommands) do --run any user-installed commands
  161.             print(line)
  162.         end
  163.     end
  164.  
  165.     if cominput[1] == "fileunload" then
  166.         loadedfile = nil
  167.         loadedfilepath = nil
  168.         comsuccess = 1
  169.         print("Unloaded file, if any.")
  170.     end
  171.  
  172.     if comsuccess == 0 then --check to see of the command was sucsessful
  173.         print("Unknown command:", cominput[1])
  174.     end
  175.  
  176. end
  177.  
  178. --Thanks to user973713 on stack overflow for this string seperator
  179. function mysplit(inputstr, sep)
  180.         if sep == nil then
  181.                 sep = "%s"
  182.         end
  183.         local t={} ; i=1
  184.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  185.                 t[i] = str
  186.                 i = i + 1
  187.         end
  188.         return t
  189. end
  190.  
  191. -- ClOS terminal mode
  192. local function advmode()
  193. print("*** STARTING CLOS IN TERMINAL MODE ***")
  194. print("Begin sys info collection")
  195. print("OS version:", osver)
  196. print("Lua version:", _VERSION, "Built for: Lua 5.1")
  197. print(" ")
  198. print("This is a BETA VERSION, don't expect things to run smoothly.")
  199. while clspgm == 0 do
  200.     io.write("ClOS > ")
  201.     io.flush()
  202.     command(mysplit(io.read(), " "))
  203. end
  204. end
  205.  
  206. -- save the list of installed commands
  207. function saveinstalllist()
  208. file = io.open("installedprograms.txt", "w")
  209. file:write("")
  210. file:close()
  211. file = io.open("installedprograms.txt", "a")
  212. for i,line in ipairs(installedcommands) do
  213.     file:write(line, "\n")
  214. end
  215. file:close()
  216. end
  217.  
  218. -- save the list of installed UIs
  219. function saveuilist()
  220. file = io.open("UIs.txt", "w")
  221. file:write("")
  222. file:close()
  223. file = io.open("UIs.txt", "a")
  224. for i,line in ipairs(installeduis) do
  225.     file:write(line, "\n")
  226. end
  227. file:close()
  228. end
  229.  
  230. -- bootloader screen will go here once I figure out how to render GUIs, for now it's hardcoded to go to advanced mode
  231. while restart == 1 do
  232.     clspgm = 0
  233.     os.execute("CLS")
  234.     advmode()
  235. end
  236.  
  237. saveinstalllist() -- save installed commands before quitting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement