home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_03 / 1n03029a < prev    next >
Text File  |  1990-04-08  |  4KB  |  110 lines

  1.  
  2. /* ---------------------------------------------------
  3.                         LISTING 2
  4.  
  5.         TEST16.C
  6.             A driver used to validate the Ctrl-C/Break
  7.             routines in ctrl_c.asm.  The C standard
  8.             library function calls use DOS Int 0x21 to
  9.             obtain keys from the keyboard buffer and to
  10.             display characters.  This gives DOS plenty
  11.             of opportunity to execute the Ctrl-C/Break
  12.             checking code within interrupt 0x21.
  13.  
  14.         Commands to produce an executable from the code as
  15.         shown in the listings:
  16.             masm ctrl_c;
  17.             cl -AL -c test16.c
  18.             link test16 ctrl_c;
  19.  
  20.         Author:  D. Burki
  21.    -------------------------------------------------- */
  22.  
  23. #include <stdio.h>
  24.  
  25. /* here we define the key which will allow the input loop
  26.    to terminate.  this key must NOT be a cursor movement
  27.    or function key.  getch() must return non zero on the
  28.    initial call when this key is pressed in order to exit
  29.    the do ... while() loop.                           */
  30.                             /* exit when enter pressed*/
  31. #define EXIT_KEY 0x0d
  32.                             /* key name for inform()  */
  33. #define EXIT_KEY_NAME "ENTER"
  34.  
  35.  
  36. int break_flag = 0;         /* flag set if Ctrl-C or  */
  37.                             /* Ctrl-Break is detected */
  38.  
  39.  
  40. void main(void);
  41. void inform(void);
  42.  
  43. extern void capture(int far *);
  44. extern void release(void);
  45.  
  46.  
  47. /*                 -----  MAIN  -----
  48.    -------------------------------------------------- */
  49. void main( void )
  50. {
  51. unsigned int ch;
  52.                             /* install the traps      */
  53.   capture((int far *)&break_flag);
  54.   inform();
  55.  
  56.   do                        /* until EXIT KEY pressed */
  57.     {
  58.     printf("If you press Ctrl-C/Break, you must also\n");
  59.     printf("press another key. capture() will eat the \n");
  60.     printf("Ctrl-C,and Ctrl-Break won't generate a key ");
  61.     printf("stroke.\nPress any key, %s to exit.\n",
  62.             EXIT_KEY_NAME);
  63.     ch = getch();
  64.     if( ch == 0 )
  65.        {
  66.        printf("Cursor movenent or function key\n\n");
  67.        getch();             /* remove 2nd part of key */
  68.        }
  69.     else
  70.        printf("Key pressed is \"%c\"\n\n",(char)ch);
  71.                             /* if flag has been set   */
  72.     if( break_flag )
  73.        {
  74.        printf("\n\tCTRL-C/BREAK DETECTED\n");
  75.  
  76. /* NOTE: the application is responsible for resetting */
  77. /*       the break flag after Ctrl-C/Break detected.  */
  78.        break_flag = 0;
  79.        }
  80.     } while( ch != EXIT_KEY );
  81.   printf("\n\nExit Key detected.  Exiting...\n");
  82.   release();                /* uninstall the int 16h,
  83.                                23h, and 1bh vectors   */
  84. }
  85.  
  86.  
  87. /*              -----  INFORM  -----
  88.     tell the user what is going to happen
  89. ----------------------------------------------------- */
  90. void inform()
  91. {
  92. int ch;
  93.  
  94.   printf("Ctrl-C and Ctrl-Break have been disabled.\n");
  95.   printf("Press any key to see the keycode & character.\n");
  96.   printf("Pressing Ctrl-C or Ctrl-Break will show that\n");
  97.   printf("these keys have been trapped.\n\n");
  98.   printf("Pressing Ctrl-2 or Alt-3 (keypad) will show\n");
  99.   printf("^C and terminate the program.  Pressing %s\n",
  100.           EXIT_KEY_NAME);
  101.   printf("will exit the input loop and restore the\n");
  102.   printf("interrupt vectors.\n\n");
  103.   printf("Press any key to begin\n\n");
  104.   ch = getch();             /* wait till key pressed  */
  105.   if( ch == 0 )             /* if function or kursor  */
  106.      ch = getch();          /* remove 2nd half        */
  107. }
  108.  
  109.  
  110.