This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AoD's profile picture

Stop operating system quitting on incorrect command

Started by AoD, 31 July 2015 - 03:18 PM
AoD #1
Posted 31 July 2015 - 05:18 PM
Hello Crafters,

I am having a few issues stopping my operating system Arc OS terminating on incorrect command, the issue is the code of stopping this happening I already have Arc OS to ignore ctrl +t but the terminate on wrong command I cant seem to rectify I've tried using the os.pullEventRaw() but upon adding that code it completely stops the os working or even boot it throws out an error.

What I need is to make the OS stop terminating until the reboot or shutdown command is given and if possible adding in a terminate command to stop the program when the user types terminate in the terminal but its a little harder than I had anticipated.


Any help would be greatly appreciated.

Kind Regards

AoD
KingofGamesYami #2
Posted 31 July 2015 - 07:56 PM
How does it terminate? Does it error?


os.pullEvent = os.pullEventRaw

Will stop control+t, but not control+r or control+s.
AoD #3
Posted 31 July 2015 - 08:26 PM
i have some commands for example help is you use hepl it stops everything and CTRL t is already disabled it errors out on the incorrect commands
flaghacker #4
Posted 31 July 2015 - 09:33 PM
I think you're looking for pcall. It basically catches all errors so you can handle them properly.
AoD #5
Posted 31 July 2015 - 10:18 PM
ok flaghacker Ill try that thank you :)/>
AoD #6
Posted 31 July 2015 - 11:53 PM
Thank you for your help much appreciated I found the issue and Flaghacker it was from the Pcall() page i found the function statements so thank you


- Edit: well it was going great till i find yes the functions are great but im useless at the pcall() i've tried it on a few commands of mine and i'm still unsure what i have done wrong could someone please help me my command code is below (the Arc OS Update in development code):



cmd1 = "shell"
cmd2 = "light"
cmd3 = "programs"
cmd4 = "exit"
cmd5 = "logout"
cmd6 = "help"
cmd7 = "credits"
cmd8 = "worldedit"
function edit()
shell.run("edit")
end
function shell()
shell.run("shell")
end

function programs()
print [[ light - Access the Lighting Sytems
shell - Access Shell Control
worldedit - Access the Worldedit Program]]
end
function light()
shell.run("programs/light")
end
function logout()
print("logging Out")
sleep(2.5)
shell.run("startup")
end
function exit()
shell.run("exit")
end
function help()
print[[ Help Section!
Here are the commands in Arc OS
help - brings up the help section
programs - shows you a list of programs installed
			inside of Arc OS ]]
end
function credits()
print [[ Arc OS - AoD
World Edit moomoomoo309 & Exlted ]]
end
function worldedit()
shell.run("programs/WE_Core")
end
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.red)
print '				   ------------'
print '					  Arc OS   '
print '				   ------------'
term.setTextColor(colors.yellow)
print("Type help for assitance using Arc OS")
print("Credits to the available programs can be accessed by typing 'credits'")
write("root@:")
cmdr = read()
if cmdr == (cmd1) then
shell()
end
if cmdr == (cmd2) then
light()
end
if cmdr == (cmd3) then
programs()
end
if cmdr == (cmd4) then
print("Exiting Arc OS")
sleep(0.5)
exit()
end
if cmdr == (cmd5) then
logout()
end
if cmdr == (cmd6) then
help()
end
if cmdr == (cmd7) then
credits()
end
if cmdr == (cmd8) then
worldedit()
end

where am I going wrong i cant see it I dont uderstand why as soon as a command is called for example say "credits" it does at its told prints the info but then the program exits and if i then give the command "help" it then says no such program.

what I'm looking for help with is to stop the os from exiting after a command is given and that pcall() they don't explain its use very clearly (or i'm stupid) but the OS exiting it a bug i need gone but its not helping me fix it could I have some help please
Edited on 31 July 2015 - 10:22 PM
Bomb Bloke #7
Posted 01 August 2015 - 12:45 AM
Lua scripts run from top to bottom - after your code has checked whatever you typed into the "cmdr = read()" line, what more instructions are there? None, and so the script ends and you're dumped back into CraftOS.

If you want to loop a section of code - running it multiple times, until eg someone types "exit" - then you'd encapsulate it within eg a "while" or "repeat" loop. Read up on Lua's control structures.

You could also make use of tables to better store your data values.
Edited on 31 July 2015 - 10:48 PM
AoD #8
Posted 01 August 2015 - 01:25 AM
Ok thank you :)/> i'll do some reading :)/>
AoD #9
Posted 01 August 2015 - 10:32 PM
Ok now I just got the weirdest out come for my while loop thats not an infinite loop but it does that anyway what I dont understand is that last night I put in the if and else if statements under "while cmdr == read() do" and the commands did nothing even coding the outcome into the if statements and even under their own function block and I didnt get the printed outcome and today I tried a different while route by giving a Isrunning = True Var at the top and then called it using "while Isrunning do" then if statements and then the printed data just repeated utin it came up with Too Long Without Yielding what every that means maybe I overloaded it Haha.

Here is the code that does the infinite loop data printout can someone help me please

Isrunning = True
cmd1 = "light"
cmd2 = "programs"
cmd3 = "exit"
cmd4 = "logout"
cmd5 = "help"
cmd6 = "credits"
cmd7 = "worldedit"
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.red)
print '				   ------------'
print '					  Arc OS   '
print '				   ------------'
term.setTextColor(colors.yellow)
print("Type help for assitance using Arc OS")
print("Credits to the available programs can be accessed by typing 'credits'")
write("root@:")
cmdr = read()
while Isrunning == True do
if cmdr == (cmd1) then
shell.run("programs/light")
elseif cmdr == (cmd2) then
print("light - Access the Lighting Sytems")
print("shell - Access Shell Control")
print("worldedit - Access the Worldedit Program")
elseif cmdr == (cmd3) then
shell.run("startup")
elseif cmdr == (cmd4) then
shell.run("startup")
elseif cmdr == (cmd5) then
print("Welcome to Arc OS you can find all the commands in this section")
print("help - This command bring up this section.")
print("programs - Gives you a list of the programs installed as Commands within Arc OS")
elseif cmdr == (cmd6) then
ptint("Arc OS - AoD")
print("WorldEdit - Moomoomoo309 and Exlted")
elseif cmdr == (cmd7) then
shell.run("programs/WE_Core")
else
print("Command not found!")
end
end
HPWebcamAble #10
Posted 01 August 2015 - 11:34 PM
The root of your problem is here:

cmdr = read()

while Isrunning == True do
...
end

When the program starts, it gets the user's input, then infinitely acts on that same input. It never tries to get the user's input again.
(The reason you get 'too long without yielding' is that you never, well, yield, that is, call os.pullEvent() in some way - like read() or sleep() does)

You can fix THAT by moving the read() into the loop:

while isRunning do

local input = read() --# I like to call this kind of variable 'input', but its up to you. Also, make things local when possible.

  if input == "cmd1" then
	--# Stuff
  elseif ...

  end
end


But like Bomb Bloke said, you can shorten this a TON with tables. You did read the link he posted, right?
Remember to ask questions if anything is confusing!


local commands = {
  ["light"] = "programs/light",
  ["exit"] = "startup",
  --# ect
}

while true do --# true is always true, so this will run forever. Also, Lua is case sensitive, so 'true' isn't the same as 'True'

  local input = read()

  if commands[input] then --# Is there a value for the key the user entered?

	shell.run( commands[input] )

  else --# User entered something we don't know what to do with
	print("Unknown command!")
  end

end


My final tip is.. Indent! It makes your code easier to read.
Edited on 01 August 2015 - 09:36 PM
AoD #11
Posted 01 August 2015 - 11:42 PM
yes I read the links that's why I went for the loop just got it majorly wrong lol I am still reading up on the tables and will look into giving them a go

Thank you everyone for your help in resolving t all the issues that rose up to destroy Arc OS but with your help I overcome the problems and I give you all respect and thanks :)/>

Arc OS Version 1 has been released
Edited on 02 August 2015 - 12:29 AM