
/*-------------------------------------------------------------*/
/* Purpose:                                                    */
/*   Enable any key to be pressed while reading and printing   */
/*   a text file to cancel the print.  Uses setjmp() and       */
/*   longjmp()                                                 */
/* Compiled:  cl /c /AL /W3  filename (Microsoft Cv5.1)        */
/*-------------------------------------------------------------*/
#pragma check_stack (off)     /* disable stack checking        */

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include <io.h>
#include <dos.h>
#include <conio.h>
#include <conio.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>

static jmp_buf mark;
static void (interrupt far *old_int09) (void);
static void (interrupt far *keyb_rtn_ptr) (void);

static int busy=0;
               /* function prototypes                     */
static void interrupt far new_int09(void);
void main(void);

/*-------------------------------------------------------------*/
/* main()
/* Purpose:                                                    */
/*   Read and print file indefinitely to test the print cancel.*/
/*-------------------------------------------------------------*/

void main()
{
int read_handle;
int prn_handle;
unsigned read_cnt;
char *buff;

read_handle = open("TEST.DAT", O_BINARY |  O_RDWR, 0);
prn_handle = fileno(stdprn);     /* get file handle                                                  
                                               for stdprn */
buff = (char *) malloc(4096);

if(setjmp(mark) != 0)            /* return here on key 
                                                    press */
          {
          printf("Cancelled print!\n");
          return;               /* print cancelled   */
          }
old_int09 = _dos_getvect(0x09); /* save old keyboard 
                                                  handler */
_dos_setvect(0x09, new_int09);  /* new keyboard 
                                                  handler */

while(1)                        /* read forever      */
          {
          read_cnt = read(read_handle, buff, 4096);
          write(prn_handle, buff, read_cnt);
                  lseek(read_handle, 0L, SEEK_SET);
          }
}

/*-------------------------------------------------------------*/
/* new_int09()                                                 */ 
/* Purpose:                                                    */
/*   Handles interrupt 09h interrupts.                         */
/*-------------------------------------------------------------*/
static void interrupt far new_int09()
{

 (*old_int09)();     /* call former interrupt handler */
 _disable();         /* disable hardware interrupts   */
                     /* while testing and setting the */
                     /* busy flag                     */

 if(busy)            /* prevent recursive invocations */
     return;
 busy = 1;
     
 _enable();          /* reenable hardware interrupts - */       
                     /* it's safe                     */

if(kbhit())         /* key in buffer                 */         
     {
     longjmp(mark, -1); /* cancel the printing           */
     }

busy = 0;
}


