🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #5760

Re: Disabling CTRL-C, in Turbo-C - not via TSR.

#5760
From: teittinen
Date: Sun, 14 Apr 1991 01:29
168 lines
7039 bytes
In article <mazz.671428317@jupiter.newcastle.edu.au>,
mazz@jupiter.newcastle.edu.au (Richard Mazzaferri) writes:
> 
> [ Suggestion 2 : install own handler for keyboard interrupt.  Wayne had
>   problems.  ( I was hoping not to do this. )  Anyone ever done it? ]

I can't believe it.  Nobody has yet posted code to do this.  Ok, I guess
it is my turn to give source to net.  The following is a group of
functions for TurboC to disable ctrl-c totally, nothing will come up on
the screen.  Or actually, you can change the ctrl-c to any scan code you
want.  But see the listing, it is quite well commented.  Hereby I
declare the following source file public domain :-).  You can use it,
but don't claim it to be yours.  Keep the comments in it. 

-------8<-------clip here-------8<-------
/***************************************************************************/
/*                                                                         */
/*  Module CtrlC                                                           */
/*                                                                         */
/*  Filename: CTRLC.C                                                      */
/*  Date:     18.09.1989                                                   */
/*  Version:  1.0                                                          */
/*  Author:   Marko Teittinen                                              */
/*                                                                         */
/*  This module contains functions to disable ctrl-c checking              */
/*                                                                         */
/*  This file is public domain, you may copy, use and modify it, but       */
/*  don't claim it to be yours. Don't remove comments, and comment your    */
/*  modifications.                                                         */
/*                                                                         */
/***************************************************************************/
/* Include files */

#include <dos.h>

/***************************************************************************/
/* Type definitions */

typedef unsigned int	word;
typedef unsigned char	byte;

/***************************************************************************/
/* Constant definitions */

#define cScan   0x2E	/* Scan code for the "C" key    */
#define CtrlOff 0xFB	/* Ctrl-C bit mask              */
#define CtrlOn  0x04	/* Ctrl-C bit mask              */

/***************************************************************************/
/* Macro definitions */

/* Get lower byte or word of a variable */
#define LOBYTE(x)	((byte)((word) x & 0x00FF))
#define LOWORD(x)	((word)((unsigned long) x & 0x0000FFFF))

/* Test if specified bit is set */
#define BITSET(x,n)	((word)x & (0x0001 << n))

/***************************************************************************/
/* Function prototypes and short documentation */

void DisableCtrlC(word ScanCode);
/* Disables Ctrl-C interrupts */
/* Given ScanCode will replace every Ctrl-C pressed */

void EnableCtrlC(void);
/* Restores original keyboard, Ctrl-C and Ctrl-Break interrupts */

/***************************************************************************/
/* Static variables */

/* Ptr to original kbd handler */
static void (interrupt far *OrigInt09)(void);
/* Ptr to original ^C  handler */
static void (interrupt far *OrigInt1B)(void);
/* Ptr to original brk handler */
static void (interrupt far *OrigInt23)(void);

/* Pointer to keyboard buffer */
static word far *KbdBuf = (word far *)0x0040001EL;
/* Pointer to keyboard control flag byte */
static word far *KbdCtrl = (word far *)0x00400017L;
/* Pointer to keyboard buffer tail pointer */
static word far *KbdTail = (word far *)0x0040001CL;

/* Value to replace Ctrl-C in keyboard buffer */
static word Value;

/***************************************************************************/
/* New keyboard interrupt */

static void interrupt far Keyboard(void)
{
	word Data,	/* Data from Kbd port 60h */
	     CtrlC,	/* CtrlC = 1, if ^C was pressed else CtrlC = 0 */
	     Indx;

	disable();

	/* Read the data from kbd port 0x60 */
	Data = inp(0x60);

	/* Test if Ctrl is pressed and C is pressed */
	if (BITSET(*KbdCtrl,2) && LOBYTE(Data) == cScan)
	{
		/* Ctrl-C was pressed */
		CtrlC = 1;
		/* Get the current position of the keyboard buffer */
		Indx = (*KbdTail - LOBYTE(LOWORD(KbdBuf))) / 2;
		/* Lie to original interrupt that Ctrl was not pressed */
		*KbdCtrl &= CtrlOff;
	}
	else
		CtrlC = 0;

	/* Call the original keyboard interrupt */
	OrigInt09();

	if (CtrlC)
	{
		/* Restore the Ctrl-flag in keyboard control flag */
		*KbdCtrl |= CtrlOn;
		/* Replace Ctrl-C with the value defined */
		KbdBuf[Indx] = Value;
	}
}

/***************************************************************************/
/* New interrupt for Ctrl-Break and Ctrl-C */

static void interrupt far DoNothing(void)
{
	/* Do nothing */
}

/***************************************************************************/
/* Disable Ctrl-C interrupts */

void DisableCtrlC(word ScanCode)
{
	OrigInt09 = getvect(0x09);  /* Save original keyboard interrupt   */
	OrigInt1B = getvect(0x1B);  /* Save original Ctrl-C interrupt     */
	OrigInt23 = getvect(0x23);  /* Save original Ctrl-Break interrupt */

	setvect(0x09, Keyboard);   /* Install our own keyboard interrupt   */
	setvect(0x1B, DoNothing);  /* Install our own Ctrl-C interrupt     */
	setvect(0x23, DoNothing);  /* Install our own Ctrl-Break interrupt */

	Value = ScanCode;
}

/***************************************************************************/
/* Enable Ctrl-C interrupts */

void EnableCtrlC(void)
{
	setvect(0x09, OrigInt09);  /* Restore original keyboard interrupt   */
	setvect(0x1B, OrigInt1B);  /* Restore original Ctrl-C interrupt     */
	setvect(0x23, OrigInt23);  /* Restore original Ctrl-Break interrupt */
}

/***************************************************************************/
/* End of file */
-------8<-------clip here-------8<-------
-- 
E-Mail: teittinen@finuh.bitnet               ! "Studying is the only way
        teittinen@cc.helsinki.fi             !  to do nothing without
Marko Teittinen, student of computer science !  anyone blaming you" -me
Technical Details
Message-ID: <1991Apr14.012906.5979@cc.helsinki.fi>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!apple!agate!ucbvax!bloom-beacon!eru!hagbard!sunic!news.funet.fi!hydra!cc.helsinki.fi!teittinen
References: <mazz.671291088@jupiter.newcastle.edu.au> <1065@masterCNA.TEK.COM> <mazz.671428317@jupiter.newcastle.edu.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!apple!agate!ucbvax!bloom-beacon!eru!hagbard!sunic!news.funet.fi!hydra!cc.helsinki.fi!teittinen
From: teittinen@cc.helsinki.fi
Newsgroups: comp.os.msdos.programmer
Subject: Re: Disabling CTRL-C, in Turbo-C - not via TSR.
Message-ID: <1991Apr14.012906.5979@cc.helsinki.fi>
Date: 14 Apr 91 01:29:06 GMT
References: <mazz.671291088@jupiter.newcastle.edu.au> <1065@masterCNA.TEK.COM> <mazz.671428317@jupiter.newcastle.edu.au>
Distribution: comp
Organization: University of Helsinki
Lines: 168