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

Program Termination

Started by DeBrates, 18 January 2016 - 02:22 PM
DeBrates #1
Posted 18 January 2016 - 03:22 PM
Keeping this short. I know that Ctrl + T terminates the program, but when I use that it also shuts down the computer. Is there something I am not familiar with, or something that I am doing wrong?

Any help is appreciated! Thanks.
Dragon53535 #2
Posted 18 January 2016 - 04:18 PM
Are you using it when no program is actually running?
KingofGamesYami #3
Posted 18 January 2016 - 05:06 PM
What are you trying to terminate? It's fairly trivial to bypass control+t if you want to.


os.pullEvent = function( ... )
  local result = { os.pullEventRaw( ... ) }
  if result[ 1 ] == "terminate" then
    os.shutdown()
  end
  return unpack( result )
end
Edited on 18 January 2016 - 10:46 PM
Bomb Bloke #4
Posted 18 January 2016 - 10:52 PM
If you hit Ctrl+T while only the base shell is running, then the system won't be running anything any more, and will indeed shut down. This much is expected behaviour.
TheOddByte #5
Posted 18 January 2016 - 11:22 PM
As those above me has said, if you're trying todo CTRL+T when the shell is running it'll shutdown the computer, if you however don't want this behaviour you could easily override the event function like this.

local pullEvent = os.pullEvent --# Create a backup of the old os.pullEvent
os.pullEvent = function( ... )
    local e = { os.pullEventRaw( ... ) }
    if e[1] == "terminated" then
        local program = shell.getRunningProgram() --# Get the current program running
        if program ~= "" then
            error( "Terminated", 0 ) --# Only throw the error "Terminated" when the program is equal to an empty string, which it returns if the program is the shell
        end
    end
    return unpack( e ) --# Return the event
end
I'm not 100% sure, but I believe that shell.getRunningProgram returns an empty string when it's the shell that's running, someone can correct me if I'm wrong.
But anyway, this would basically allow it to terminate regular programs, but not shutdown the computer if you're doing CTRL+T, that's because it doesn't throw an
error when it's in the shell.

Another thing you should take a note on is that I haven't tested this code-snippet, so bare that in mind if it doesn't work.