Catching CTRL-C

[Autolink] Menu

In REXX programs that don't contain an error handler for CTRL-C, CTRL-C and CTRL-BREAK are processed by the REXX interpreter using the default error handler for CTRL-C. The default error handler for CTRL-C simply prints an error message and ends the program. Here are some ways to make the handling of CTRL-C und CTRL-BREAK more useful.

To install your own error handler you can use either

 
  signal on halt name myCTRLCHandler

or

 
  call on halt name myCTRLCHandler

The adavantage of the call statement is that you can use the RETURN instruction in your CTRL-C handler to continue the execution of your program. Thus, using this method, it is possible, for example, to suppress CTRL-C depending on user input, to display specific messages, and so on (see the examples below).

Note: Because of a bug in the CTRL-C handling of the CMD.EXE you should always use

 
  cmd /c os2_command

to execute internal or external OS/2 commands from within a REXX program (see CTRL-Break & OS/2 commands, see also Information saved during subroutine & function execution).

Examples:

 
/* sample for a CTRL-C error handler using SIGNAL ON HALT ...       */

                    /* install the error handler for CTRL-C         */
  signal on halt name MyCtrlCHandler

                    /* this is an endless loop                      */
  do forever
     say 'Press CTRL-C to end this program ...'
  end /* do forever */

exit

/* error handler for CTRL-C                                         */
/*                                                                  */
/* This error handler simply ends the program.                      */
/*                                                                  */
MyCtrlCHandler:

                    /* reinstall the error handler for CTRL-C       */
  signal on halt name MyCtrlCHandler

  say 'CTRL-C pressed!'
exit

 
/* sample for a CTRL-C error handler using CALL ON HALT ...         */

                    /* install the error handler for CTRL-C         */
  call on halt name MyCtrlCHandler

  ctrlCPressed = 0

                    /* this is an endless loop                      */
  do until CtrlCPressed = 1
     say 'Press CTRL-C to end this program ...'
  end /* do until CtrlCPressed */

  say 'Program ends now'

                    /* end the program                              */
exit

/* error handler for CTRL-C                                         */
MyCtrlCHandler:

                    /* reinstall the error handler for CTRL-C       */
  signal on halt name MyCtrlCHandler

  say 'CTRL-C pressed (Execution was interrupted in line ' || sigl || ')!'
  say 'Press Y <RETURN> to end the program. Any other key to continue ...'
  if translate( strip( lineIn() ) ) = 'Y' then
  do
                    /* signal "end now" to the main program         */
    ctrlCPressed = 1
  end /* if */
                    /* continue the program at the interupted line  */
return


[Back: Returning more than one value from a function]
[Next: Redefinition of internal functions]