CTRL+C Handler (Interrupt 23h) carries out program-specific actions in response to the CTRL+C (ASCII 03h) key combination being pressed. MS-DOS issues this interrupt if it receives the CTRL+C character while processing a system function. The handler carries out its actions then returns to the system in order to restart the system function or terminate the current program.
Programs must not issue Interrupt 23h.
MS-DOS sets the current CTRL+C handler when starting a program, copying the address of the parent program's handler to both the vector-table entry and offset 0Eh in the new program's PSP (pspControlCVector field). Although a program can change the vector-table entry, it must not change the address in its PSP, since MS-DOS uses this address to restore the parent program's handler.
MS-DOS does not immediately issue Interrupt 23h when the user presses the CTRL+C key combination. Instead, the system places the CTRL+C character (ASCII 03h) in the keyboard buffer; if no other characters are ahead of the control character, the system processes it while carrying out a system function. For most computers, MS-DOS also places a CTRL+C character in a buffer when the user presses the CTRL+BREAK key combination. Pressing this combination places a CTRL+C character ahead of all other characters in the keyboard buffer.
MS-DOS checks for the CTRL+C character while carrying out character I/O functions (Interrupt 21h Functions 01h through 0Ch). It also checks for the character while carrying out other system functions—but only if the CTRL+C check flag is set. If the I/O mode for the keyboard (or input device) is binary, the system disables CTRL+C character processing while a program uses Read File or Device (Interrupt 21h Function 3Fh) and the CTRL+C character is read as input.
Before issuing Interrupt 23h, MS-DOS does the following:
Sets all registers to the values they had when the interrupted system function was initially called.
Sets the program's stack to be the current stack. When the handler receives control, the stack has the following contents (from the top of the stack):
The return address (CS:IP) and the flags needed for the iret
instruction back to the system.
The return address (CS:IP) and the flags needed for the iret
instruction back to the program.
Sets to zero any internal system variables, such as the ErrorMode and InDOS variables, so that the handler can call system functions or even return directly to the program without disrupting system operations.
A CTRL+C handler can call any system function.
Upon returning from Interrupt 23h, MS-DOS checks the method of return to determine what action to take. If the handler sets the carry flag and returns with the retf instruction, MS-DOS terminates the program by calling End Program (Interrupt 21h Function 4Ch). If the handler returns with the iret instruction or with the retf instruction after clearing the carry flag, the system repeats the call to the system function, starting the function's action again from the beginning. In this case, the handler must preserve all registers, restoring them before returning to the system.
COMMAND.COM provides the default CTRL+C handler, which terminates the current program unless a batch file is running, in which case the handler prompts the user to continue (or not) with the next command in the file.
Interrupt 21h Function 3Fh Read File or Device
Interrupt 21h Function 4Ch End Program