home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msuchk.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  19KB  |  442 lines

  1. /* scanchek.c */
  2. /*
  3. ** By: Phil Benchoff, June 1986
  4. ** v2.0: 31 Aug 1987, Update Kermit to v2.29C, add Turbo-C support.
  5. ** v3.0: 31 Aug 1987 merge header and main body, telescope code, add
  6. **    some revisons and Bios support for most C compilers. Joe R. Doupnik
  7. ** v3.1: 01 Sep 1987, add #ifndef before bioskey() function.
  8. ** v4.0: 22 Jan 1988, Computer Innovations C86 support dropped.
  9. **                    Enhanced keyboard support added
  10. **                    Mods to match MS-Kermit 2.30
  11. **                    A few JRD suggestions
  12. ** v4.1: 20 Jan 1989, fix bug with NumLock/Shift, match MS-Kermit 2.32
  13. **                    Matthias Reichling
  14. **
  15. ** This program displays various information about the keys pressed.
  16. ** It's original intent is to be used in testing the new IBM keyboards
  17. ** (you know the ones, with the F keys on the top).
  18. **
  19. ** Enhanced keyboard:
  20. ** For more information on the enhanced keyboard see:
  21. ** 'Keying on a Standard', _PC_TECH_JOURNAL_, July 1987, pp. 134-156
  22. ** Minor error in article,  Table 1:  Numeric Keypad:
  23. **    Buffer codes with lsb=0xF0 are actually translated to have
  24. **    lsb=00 by int 0x16.
  25. **
  26. ** The BIOS interrupt 0x16 is used for keyboard services. The particular
  27. ** service is determined by the service code in AH when the interrupt
  28. ** is invoked.
  29. **
  30. ** AH = 0 returns the next available keyboard character.
  31. **        AL = extended-ASCII character code
  32. **        AH = keyboard scan code ( 0 if the alt-numeric input used )
  33. **  or
  34. **        AL = 0
  35. **        AH = special character code ( F1-F10, Home, End, etc.)
  36. ** AH = 1 test for a character ( don't wait )
  37. **        returned values same as above
  38. ** AH = 2 returns the current shift states
  39. **        AL = shift states
  40. ** AH = 0x10 or 0x11
  41. **        enhanced keyboard versions of AH=0 and AH=1
  42. **
  43. ** The MS-Kermit (2.32) 'key ident' is also printed.  This value
  44. ** is used in the SET KEY command.  Note that Kermit uses the shift
  45. ** bits differently than the BIOS routines,  so multiple shifts
  46. ** (e.g. Ctrl-Alt-F1) can be used with the same key in some cases.
  47. */
  48.  
  49. #include <stdio.h>
  50. #include <dos.h>
  51.  
  52. /* Kermit-MS keyboard shift flags (most significant byte of key_ident) */
  53. #define SCAN    0x0100  /* scan code */
  54. #define SHIFT   0x0200  /* left or right shift */
  55. #define CONTROL 0x0400  /* Ctrl shift */
  56. #define ALT     0x0800  /* Alt shift */
  57. #define ENHANCE 0x1000  /* Enhanced keyboard special key */
  58.  
  59. #define BS 0x08
  60. #define TAB 0x09
  61. #define EOS '\0'
  62. /*
  63. ** These two tables are useful for relating ascii and scan codes
  64. ** to the characters entered or keys pressed.
  65. ** There are several types of characters included.
  66. ** ASCII characters:
  67. ** - Characters 0x00 to 0x1F are control characters.
  68. **   The array ascii_chars[i] contains the names of these
  69. **   control characters. 'Space' is included for lack of
  70. **   a better place to put it. The index i should be
  71. **   the ascii code for the character. Note that the ascii
  72. **   character NUL (0x00) cannot be entered on the IBM-PC keyboard.
  73. ** - The character codes 0x20 - 0xff are printable on the
  74. **   IBM-PC and are not considered here (except 'space').
  75. ** Special characters:
  76. **   For some characters, no ascii value is returned by BIOS interrupt
  77. **   0x16. The scan codes of these characters identify them.
  78. **   They include F keys, arrow keys, and alt keys.
  79. **   With the enhanced keyboard,  BIOS may return the following:
  80. **   - scancode = 0xe0, ascii = non-enhanced scancode
  81. **   - scancode = non-enhanced scancode, ascii = 0xe0 of 0xf0
  82. **
  83. **   The array special_chars[i] contains tha names of these keys.
  84. **   The index i should be the scan code for the key.
  85. **   The array is 166 elements long, but not all of the
  86. **   scan codes in that range are special keys.
  87. **
  88. ** Phil Benchoff, June 1986
  89. ** revised Jan 88
  90. */
  91.  
  92. char *ascii_chars[] = {                 /* ANSI names of control codes */
  93.    "NUL", "SOH", "STX", "ETX", "EOT", "ENQ",  /* ^@, ^A, ^B, ^C, ^D, ^E */
  94.    "ACK", "BEL", "BS" , "HT" , "LF" , "VT",   /* ^F, ^G, ^H, ^I, ^J, ^K */
  95.    "FF" , "CR" , "SO" , "SI" , "DLE", "DC1",  /* ^L, ^M, ^N, ^O, ^P, ^Q */
  96.    "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",  /* ^R, ^S, ^T, ^U, ^V, ^W */
  97.    "CAN", "EM" , "SUB", "ESC", "FS" , "GS",   /* ^X, ^Y, ^Z, ^[, ^\, ^] */
  98.    "RS" , "US" , "Space" } ;                  /* ^^, ^_, space */
  99.  
  100. char *special_keys[] = {            /* common text for given scan code */
  101.    "", "Alt-Esc", "", "NUL", "", "", "", "",                    /* 0-7 */
  102.    "", "", "Ctrl-Num-Ent", "", "Alt-Enter", "Num-Enter",        /* 8-13 */
  103.    "Alt-BS", "Shift-Tab",                                       /* 14-15 */
  104.    "Alt-Q", "Alt-W", "Alt-E", "Alt-R", "Alt-T",                 /* 16-20 */
  105.    "Alt-Y", "Alt-U", "Alt-I", "Alt-O", "Alt-P",                 /* 21-25 */
  106.    "Alt-[", "Alt-]", "Alt-Enter", "",                           /* 26-29 */
  107.    "Alt-A", "Alt-S", "Alt-D", "Alt-F", "Alt-G",                 /* 30-34 */
  108.    "Alt-H", "Alt-J", "Alt-K", "Alt-L",                          /* 35-38 */
  109.    "Alt-;", "Alt-'", "Alt-`", "", "Alt-\\",                     /* 39-43 */
  110.    "Alt-Z", "Alt-X", "Alt-C", "Alt-V", "Alt-B",                 /* 44-48 */
  111.    "Alt-N", "Alt-M",                                            /* 49-50 */
  112.    "Alt-,", "Alt-.", "Alt-/", "", "Alt-Num-*", "", "", "",      /* 51-58 */
  113.    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", /* 59-68 */
  114.    "", "",                                                      /* 69-70 */
  115.    "Home", "U-arrow", "PgUp", "Alt-NumMinus",                   /* 71-74 */
  116.    "L-arrow", "Num-5", "R-arrow",                               /* 75-77 */
  117.    "Alt-Num-+", "End", "D-arrow", "PgDn", "Ins", "Del",         /* 78-83 */
  118.    "Shift-F1", "Shift-F2", "Shift-F3", "Shift-F4", "Shift-F5",  /* 84-88 */
  119.    "Shift-F6", "Shift-F7", "Shift-F8", "Shift-F9", "Shift-F10", /* 89-93 */
  120.    "Ctrl-F1",  "Ctrl-F2",  "Ctrl-F3",  "Ctrl-F4",  "Ctrl-F5",   /* 94-98 */
  121.    "Ctrl-F6",  "Ctrl-F7",  "Ctrl-F8",  "Ctrl-F9",  "Ctrl-F10",  /* 99-103 */
  122.    "Alt-F1",   "Alt-F2",   "Alt-F3",   "Alt-F4",   "Alt-F5",    /* 104-108 */
  123.    "Alt-F6",   "Alt-F7",   "Alt-F8",   "Alt-F9",   "Alt-F10",   /* 109-113 */
  124.    "Ctrl-PrtSc", "Ctrl-L-arrow", "Ctrl-R-arrow",                /* 114-116 */
  125.    "Ctrl-End",   "Ctrl-PgDn",    "Ctrl-Home",                   /* 117-119 */
  126.    "Alt-1", "Alt-2", "Alt-3", "Alt-4", "Alt-5", "Alt-6",        /* 120-125 */
  127.    "Alt-7", "Alt-8", "Alt-9", "Alt-0", "Alt--", "Alt-=",        /* 126-131 */
  128.    "Ctrl-PgUp", "F11", "F12", "Shift-F11",  "Shift-F12",        /* 132-136 */
  129.    "Ctrl-F11", "Ctrl-F12", "Alt-F11", "Alt-F12",                /* 137-140 */
  130.    "Ctrl-U-arrow", "Ctrl-NumMinus", "Ctrl-Num-5",               /* 141-143 */
  131.    "Ctrl-Num-+", "Ctrl-D-Arrow", "Ctrl-Insert",                 /* 144-146 */
  132.    "Ctrl-Delete", "Ctrl-Tab", "Ctrl-Num-/", "Ctrl-Num-*",       /* 147-150 */
  133.    "Alt-Home", "Alt-U-arrow", "Alt-PgUp", "",                   /* 151-154 */
  134.    "Alt-L-arrow", "", "Alt-R-Arrow", "", "Alt-End",             /* 155-159 */
  135.    "Alt-D-Arrow", "Alt-PgDn", "Alt-Insert", "Alt-Del",          /* 160-163 */
  136.    "Alt-Num-/", "Alt-Tab", "Alt-Num-Ent."                       /* 164-166 */
  137. };
  138. #define NSPECIALS 166  /* length of special_keys[] */
  139.  
  140.  
  141. main()
  142. {
  143.    unsigned int count, ascii, scan;
  144.    int read_fn, enh_key;
  145.    char *comment();
  146.    unsigned int bios_key, bios_shifts, key_ident, kerm_key();
  147.  
  148.    explain();  /* display instructions */
  149.  
  150.    /*
  151.    ** Determine if we have an enhanced keyboard.
  152.    ** Select the function to use when calling the keyboard interrupt.
  153.    **  read_fn = peekb(seg,offset) may need replacing by
  154.    **  peek(seg,offset,&read_fn,1); read_fn &= 0x10;  for Lattice C.
  155.    */
  156.    read_fn = peekb(0x40,0x96) & 0x10; /* get BIOS enhanced keyboard flag */
  157.              /* read_fn is either 0x00 or 0x10 */
  158.    if ( 0 == read_fn ) {              /* flag = 0,  standard keyboard */
  159.       printf("Standard keyboard:\n");
  160.    }
  161.    else {                             /* flag = 1, enhanced keyboard */
  162.       printf("Enhanced keyboard:\n");
  163.    }
  164.  
  165.    count = 1;
  166.  
  167.    while ( 1 ) {
  168.       if (1 == count ) {
  169.          header();
  170.          count = 21;
  171.       } else
  172.          count--;
  173.  
  174.       enh_key=0; /* set this to 1 in the following code if this key */
  175.                  /* is only available on the enhanced keyboard */
  176.       bios_key = bioskey(read_fn);
  177.       bios_shifts = bioskey(2);
  178.  
  179.       ascii = bios_key & 0x00FF;     /* ASCII value is in low byte */
  180.       scan  = bios_key >> 8;         /* Scan code is in high byte  */
  181.       key_ident = kerm_key(bios_key,bios_shifts);
  182.  
  183.       /* Note: You can't enter a NUL (ascii 00) from the keyboard */
  184.  
  185.       if ( 0xe0 == scan ) {                       /* enhanced keyboard */
  186.          enh_key = 1;
  187.          /*
  188.          ** The IBM memorial exception:
  189.          ** The buffer code for Numeric-/ is 0xe02f; however 0x2f is
  190.          ** the scan code for Alt-V,  so we can't just use al as the
  191.          ** scan code.
  192.          */
  193.          if ( 0x2f == ascii )
  194.             printf("| %-13s ", "Numeric-/");
  195.          else if (ascii <= NSPECIALS)
  196.             printf("| %-13s ", special_keys[ascii]);
  197.          else
  198.             printf("| * unknown *   ");
  199.       } else if ( (0!=scan) && (0xe0==ascii) ) {  /* enhanced keyboard */
  200.          enh_key = 1;
  201.          if (scan <= NSPECIALS)
  202.             printf("| %-13s ", special_keys[scan]);
  203.          else
  204.             printf("| * unknown *   ");
  205.       } else if ( (ascii >= 33) && (ascii <= 255) ) {  /* Printable char. */
  206.          printf("| %-13c ", ascii );
  207.       } else if ( (ascii > 0) && (ascii <= 31) ) {  /* control char */
  208.          printf("| Ctrl-%c %-6s ", ascii + 64, ascii_chars[ascii]);
  209.       } else if (ascii == 32) {                /* Space       */
  210.          printf("| Space         ");
  211.       } else if ( ascii == 0 ) {      /* special key (no ascii value) */
  212.          if (scan <= NSPECIALS) {              /* in table? */
  213.             if ( 0x84 < scan )        /* enhanced keyboard if scan > 84 */
  214.                enh_key = 1;
  215.             else {
  216.                switch (scan) {
  217.                   /*
  218.                   ** The following keys produce a scan code and
  219.                   ** no ascii value,  but are only available on
  220.                   ** the enhanced keyboard.
  221.                   */
  222.                   case 0x01:      /* Alt-ESC */
  223.                   case 0x0e:      /* Alt-Backspace */
  224.                   case 0x1a:      /* Alt-[ */
  225.                   case 0x1b:      /* Alt-] */
  226.                   case 0x1c:      /* Alt-Enter */
  227.                      /* Alt-Enter produces scancode 12 (dec.) on my */
  228.                      /* PC Limited AT-8 w/standard keyboard */
  229.                   case 0x27:      /* Alt-; */
  230.                   case 0x28:      /* Alt-' */
  231.                   case 0x29:      /* Alt-` */
  232.                   case 0x2b:      /* Alt-\ */
  233.                   case 0x33:      /* Alt-, */
  234.                   case 0x34:      /* Alt-. */
  235.                   case 0x35:      /* Alt-/ */
  236.                   case 0x37:      /* Alt-* */
  237.                   case 0x4a:      /* Alt-Num- */
  238.                   case 0x4c:      /* Num-5 */
  239.                   case 0x4e:      /* Alt-Num+ */
  240.                      enh_key = 1;
  241.                      break;
  242.                   default:
  243.                      break;
  244.                };
  245.             }
  246.             printf("| %-13s ", special_keys[scan]);
  247.          }
  248.          else
  249.             printf("| * unknown *   ");
  250.       } else {
  251.          printf("| Out of range  ");
  252.       }
  253.  
  254.       printf("| %3d | 0x%02x |%c| \\%-4d ", scan, ascii,
  255.               enh_key ? '*' : ' ', key_ident);
  256.  
  257.       /* print Kermit shift bit status */
  258.       if ( SCAN & key_ident ) {
  259.          printf("| %c", (key_ident & ENHANCE) ? 'E' : '-');
  260.          printf("%c",(key_ident & ALT) ? 'A' : '-');
  261.          printf("%c",(key_ident & CONTROL) ? 'C' : '-');
  262.          printf("%c ",(key_ident & SHIFT) ? 'S' : '-');
  263.       } else printf("|      ");
  264.  
  265.       printf("| %-29s|\n", comment(scan,ascii) );
  266.         if (key_ident == 1280) break;         /* Control-Break exit */
  267.    }
  268.    exit(0);
  269. }
  270.  
  271. /*
  272. ** This function should match 'getkey' in MSUIBM.ASM.
  273. **
  274. ** Kermit-MS determines the 'key ident' from the value returned in ax
  275. ** (ah=scan code, al=ascii value) from BIOS call 0x16 function 0
  276. ** (standard keyboard) or 0x10 (enhanced keyboard), the
  277. ** status of various shift keys, and a table of special keys (aliaskey).
  278. ** The aliaskey table handles cases where more than one key may generate
  279. ** the same ascii value (i.e. the numeric keypad).  The entries in table
  280. ** aliaskey are words with the high byte holding a scan code and the low
  281. ** byte holding an ascii value.  The general method is as follows:
  282. **
  283. **    BIOS int 0x16 function 0 or 0x10   returns key's code in register ax.
  284. **    if (ah!=0 and al=0xe0)             enhanced
  285. **       al = 0                          zero ascii
  286. **       key_ident |= ENHANCE            set enhanced flag
  287. **    else if (ah==0xe0)                 enhanced
  288. **       ah=al                           put scan in ah
  289. **       al=0                            zero ascii
  290. **       key_ident |= ENHANCE            set enhanced flag
  291. **    if (ax is in aliaskey list)
  292. **       al = 0                    clear normal ascii to simulate special key
  293. **    ascii = al                         do this in either case
  294. **    if ( ascii == 0 ) {                now, if the key is a special one ...
  295. **    scancode = ah                      work with scan code instead
  296. **       key_ident = scancode + SCAN     set SCAN code flag bit
  297. **       if ( (LeftShift || RightShift) && (no NumKeypadWhiteKey)
  298. **           || (NumKeypadWhiteKey && NumLock && no Shift)
  299. **           || (NumKeypadWhiteKey && no NumLock && Shift) )
  300. **          key_ident |= SHIFT           set smart SHIFT key flag bit
  301. **       if ( Ctrl )
  302. **          key_ident |= CONTROL         set CONTROL key flag bit
  303. **       If ( Alt )
  304. **          key_ident |= ALT             set ALT key flag bit
  305. **    } else
  306. **       key_ident = ascii
  307. */
  308. unsigned int kerm_key(bios_key,bios_shifts)
  309. unsigned int bios_key, bios_shifts;
  310. {
  311.    unsigned  key_id, shift_hlp;
  312.  
  313.    key_id = 0;
  314.    shift_hlp = 0;
  315.    /* getky1a */
  316.    if ( 0 != (bios_key & 0xff00) ) {        /* not alt-numeric */
  317.       if ( 0xe000 == (bios_key & 0xff00) ) {
  318.          bios_key <<= 8;
  319.          key_id |= ENHANCE;
  320.       }
  321.       /* getky1b */
  322.       if ( 0x00e0 == (bios_key & 0x00ff) ) {
  323.          bios_key &= 0xff00;
  324.          key_id |= ENHANCE;
  325.       }
  326.    }
  327.    if ( 0 != (bios_key & 0x00ff) ) {        /* not scan code only */
  328.       /* getky1c */
  329.       /* aliaskey processing */
  330.       switch (bios_key)
  331.       {
  332.          case ( 14 * SCAN ) + BS:
  333.          case ( 55 * SCAN ) + '*':
  334.          case ( 74 * SCAN ) + '-':
  335.          case ( 78 * SCAN ) + '+':
  336.          case ( 71 * SCAN ) + '7':
  337.          case ( 72 * SCAN ) + '8':
  338.          case ( 73 * SCAN ) + '9':
  339.          case ( 75 * SCAN ) + '4':
  340.          case ( 76 * SCAN ) + '5':
  341.          case ( 77 * SCAN ) + '6':
  342.          case ( 79 * SCAN ) + '1':
  343.          case ( 80 * SCAN ) + '2':
  344.          case ( 81 * SCAN ) + '3':
  345.          case ( 82 * SCAN ) + '0':
  346.          case ( 83 * SCAN ) + '.':
  347.          case ( 83 * SCAN ) + ',':
  348.          case ( 15 * SCAN ) + TAB:
  349.                    bios_key &= 0xff00;     /* clear ascii low byte */
  350.                    break;
  351.          default:                        /* key is not in the list */
  352.                    break;
  353.       };
  354.    }
  355.    if ( (bios_key & 0x00ff) == 0 ) {
  356.                            /* No ASCII value, get a kermit scan code. */
  357.       key_id |= ((bios_key >> 8) | SCAN);       /* set scancode flag */
  358.       if (bios_shifts & 3)                      /* left or right shift?*/
  359.          shift_hlp |= SHIFT;                    /* yes, set shift flag */
  360.       if ((bios_shifts & 0x20)                  /* NumLock set? */
  361.              && ( key_id >= ( 71 + SCAN ) )     /* on numeric keypad ? */
  362.              && ( key_id <= ( 83 + SCAN ) )
  363.              && ( key_id != ( 74 + SCAN ) )     /* not the grey - key? */
  364.              && ( key_id != ( 78 + SCAN ) ) )   /* not the grey + key? */
  365.          shift_hlp ^= SHIFT;                    /* all true, xor shift */
  366.       key_id |= shift_hlp;
  367.       if (bios_shifts & 0x04)                   /* Ctrl-key pressed? */
  368.          key_id |= CONTROL;
  369.       if (bios_shifts & 0x08)                   /* Alt-key pressed? */
  370.          key_id |= ALT;
  371.    } else
  372.       /* We have an ASCII value,  return that */
  373.       key_id = bios_key & 0xff;
  374.  
  375.    return key_id;
  376. }
  377.  
  378. char *comment(scan,ascii)
  379. int  scan, ascii;
  380. {
  381.     static char line[40];
  382.  
  383.     line[0] = '\0';                     /* start with an empty line */
  384.  
  385.    if ( (0xe0 == ascii) && (0 != scan) ) {
  386.       ascii = 0;
  387.    } else if ( 0xe0 == scan ) {
  388.       scan = ascii;
  389.       ascii = 0;
  390.    }
  391.     if ( (ascii !=0) && (scan >= 71) )
  392.        strcpy(line,"Numeric Keypad");
  393.     else if ( (ascii == 0) &&  (scan != 0) )
  394.        strcpy(line,"Special Key");
  395.     else if ( (ascii != 0) && (scan == 0) )
  396.        strcpy(line,"Alt-numeric method.");
  397.     return(line);
  398. }
  399.  
  400. header() {
  401.    char *dash38 = "--------------------------------------",
  402.         *left1  = "|           B I O S            |Kermit-",
  403.         *right1 = "MSv2.32|                              |",
  404.         *left2  = "|Key            |Scan |ASCII |E|KeyIdnt",
  405.         *right2 = "|Flags |Notes                         |";
  406.  
  407.    printf("+%s%s+\n%s%s\n%s%s\n+%s%s+\n",dash38,dash38,left1,right1,
  408.            left2,right2,dash38,dash38);
  409.  
  410. }
  411.  
  412. explain()
  413. {
  414. /* Tell what the program does, and how to get out */
  415.    printf("ScanChek 4.1 (20 Jan 89)\n\n");
  416.    printf("This program displays the scan code, ASCII code, and\n");
  417.    printf("the Kermit 'key ident' used in the Kermit-MS SET KEY command. ");
  418.    printf("Keycodes are \nobtained from the IBM-PC keyboard BIOS interrupt");
  419.    printf(" 16H, function 0 or 0x10.\n");
  420.    printf("Do not type ahead of the display.\n\n");
  421.    printf("Key    - The key pressed. Determined from ascii value or");
  422.    printf(" BIOS scan code.\n");
  423.    printf("Scan   - The BIOS scan code (ah).\n");
  424.    printf("ASCII  - The ASCII value (al).\n");
  425.    printf("E      - * if available on enhanced keyboard only.\n");
  426.    printf("KeyIdnt- The Kermit-MS v2.32 key ident.\n");
  427.    printf("Flags  - Flags present in KeyIdnt. Valid only if \"Kermit\" field");
  428.    printf(" is > 255\n         (E=Enhanced, A=Alt, C=Ctrl, S=Shift)\n");
  429.    printf("E      - * if key ident enhanced bit is set.\n");
  430.    printf("\nPress Control-BREAK to exit.\n\n");
  431. }
  432.  
  433. bioskey(function)                       /* For most C compilers [jrd] */
  434. int function;                           /* do Interrupt 16H via int86() */
  435. {
  436.    static union REGS reg;                       /* for Bios int86 calls */
  437.    reg.h.ah = function;
  438.    int86(0x16, ®, ®);
  439.    return (reg.x.ax);
  440. }
  441.  
  442.