1+ -- command.lua, a very basic command interpreter for miniOS
2+ -- are we in miniOS?
3+ if not miniOS then error (" This program requires miniOS!" ) return end
4+
5+ local shell = {}
6+
7+ local tArgs = {... }
8+ local continue
9+ if tArgs [1 ] == " -c" then continue = true table.remove (tArgs , 1 ) end
10+
11+ local function intro () print (_OSVERSION .. " [" .. math.floor (computer .totalMemory ()/ 1024 ).. " k RAM, at least " .. math.floor (miniOS .freeMem / 1024 ).. " k Free]" .. " \n Command Interpreter By Skye M.\n " ) end
12+ if not continue then intro () end
13+
14+ local history = {}
15+ if miniOS .cmdHistory then history = miniOS .cmdHistory end
16+ if miniOS .cmdDrive then fs .drive .setcurrent (miniOS .cmdDrive ) end
17+
18+ local function runprog (file , parts )
19+ miniOS .cmdHistory = history
20+ miniOS .cmdDrive = fs .drive .getcurrent ()
21+ table.remove (parts , 1 )
22+ error ({[1 ]= " INTERRUPT" , [2 ]= " RUN" , [3 ]= file , [4 ]= parts })
23+ end
24+
25+ local function runbat (file , parts )
26+ error (" Not yet Implemented!" )
27+ end
28+
29+ local function listdrives ()
30+ for letter , address in fs .drive .list () do
31+ print (letter , address )
32+ end
33+ end
34+
35+ local function lables ()
36+ for letter , address in fs .drive .list () do
37+ print (letter , component .invoke (address , " getLabel" ))
38+ end
39+ end
40+
41+ local function label (parts )
42+ proxy , reason = fs .proxy (parts [2 ])
43+ if not proxy then
44+ print (reason )
45+ return
46+ end
47+ if # parts < 3 then
48+ print (proxy .getLabel () or " no label" )
49+ else
50+ local result , reason = proxy .setLabel (parts [3 ])
51+ if not result then print (reason or " could not set label" ) end
52+ end
53+ end
54+
55+ local function outputFile (file , paged )
56+ local handle , reason = filesystem .open (file )
57+ if not handle then
58+ error (reason , 2 )
59+ end
60+ local buffer = " "
61+ repeat
62+ local data , reason = filesystem .read (handle )
63+ if not data and reason then
64+ error (reason )
65+ end
66+ buffer = buffer .. (data or " " )
67+ until not data
68+ filesystem .close (handle )
69+ if paged then printPaged (buffer )
70+ else print (buffer ) end
71+ end
72+
73+ local function dir (folder )
74+ -- we will have to get the current dir later (we will need fs.resolve!)
75+ folder = " /" .. (folder or " " )
76+ -- is it a directory?
77+ if not fs .isDirectory (folder ) then print (" No such folder." ) return end
78+ -- if it is we start...
79+ local output = " "
80+ -- put the list of files into a massive string
81+ for file in filesystem .list (folder ) do output = output .. file .. " \n " end
82+ -- get rid of the last newline
83+ output = output :sub (0 , - 2 )
84+ -- we want the output to be paged
85+ printPaged (output )
86+ end
87+
88+ local function runline (line )
89+ line = text .trim (line )
90+ if line == " " then return true end
91+ parts = text .tokenize (line )
92+ command = string.lower (text .trim (parts [1 ]))
93+ -- blank commands
94+ if command == " " then return true end
95+ if command == nil then return true end
96+ -- drive selector
97+ if # command == 2 then if string.sub (command , 2 , 2 ) == " :" then filesystem .drive .setcurrent (string.sub (command , 1 , 1 )) return true end end
98+ -- external commands and programs
99+ command = parts [1 ]
100+ if filesystem .exists (command ) then
101+ if not filesystem .isDirectory (command ) then
102+ if text .endswith (command , " .lua" ) then runprog (command , parts ) return true end
103+ if text .endswith (command , " .bat" ) then runbat (command , parts ) return true end
104+ runprog (command , parts ) return true
105+ end
106+ end
107+ if filesystem .exists (command .. " .lua" ) then
108+ if not filesystem .isDirectory (command .. " .lua" ) then
109+ runprog (command .. " .lua" , parts )
110+ return true
111+ end
112+ end
113+ if filesystem .exists (command .. " .bat" ) then
114+ if not filesystem .isDirectory (command .. " .bat" ) then
115+ runbat (command .. " .bat" , parts )
116+ return true
117+ end
118+ end
119+ -- internal commands
120+ if command == " exit" then history = {} return " exit" end
121+ if command == " cls" then term .clear () return true end
122+ if command == " ver" then print (_OSVERSION ) return true end
123+ if command == " mem" then print (math.floor (computer .totalMemory ()/ 1024 ).. " k RAM, " .. math.floor (computer .freeMemory ()/ 1024 ).. " k Free" ) return true end
124+ if command == " dir" then dir () return true end
125+ if command == " intro" then intro () return true end
126+ if command == " disks" then listdrives () return true end
127+ if command == " discs" then listdrives () return true end
128+ if command == " drives" then listdrives () return true end
129+ if command == " labels" then lables () return true end
130+ if command == " label" then if parts [2 ] then label (parts ) return true else print (" Invalid Parameters" ) return false end end
131+ if command == " type" then outputFile (parts [2 ]) return true end
132+ if command == " more" then outputFile (parts [2 ], true ) return true end
133+ if command == " echo" then print (table.concat (parts , " " , 2 )) return true end
134+ if command == " cmds" then printPaged ([[
135+ Internal Commands:
136+ exit --- Exit the command interpreter, Usually restarts it.
137+ cls ---- Clears the screen.
138+ ver ---- Outputs version information.
139+ mem ---- Outputs memory information.
140+ dir ---- Lists the files on the current disk.
141+ cmds --- Lists the commands.
142+ intro -- Outputs the introduction message.
143+ drives - Lists the drives and their addresses.
144+ labels - Lists the drives and their labels.
145+ label -- Sets the label of a drive.
146+ echo --- Outputs its arguments.
147+ type --- Like echo, but outputs a file.
148+ more --- Like type, but the output is paged.
149+ copy --- Copies a file.
150+ move --- Moves a file.]] ) print () return true end
151+ print (" '" .. parts [1 ] .. " ' is not an internal or external command, program or batch file." )
152+ return false
153+ end
154+
155+ function shell .runline (line )
156+ local result = table.pack (pcall (runline , line ))
157+ if result [1 ] then
158+ return table.unpack (result , 2 , result .n )
159+ else
160+ if type (result [2 ]) == " table" then if result [2 ][1 ] == " INTERRUPT" then error (result [2 ]) end end
161+ printErr (" ERROR:" , result [2 ])
162+ end
163+ end
164+
165+ if shell .runline (table.concat (tArgs , " " )) == " exit" then return end
166+
167+ while true do
168+ term .write (filesystem .drive .getcurrent () .. " >" )
169+ local line = term .read (history )
170+ while # history > 10 do
171+ table.remove (history , 1 )
172+ end
173+ if shell .runline (line ) == " exit" then return end
174+ end
0 commit comments