home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / KBSTUFF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  4.6 KB  |  161 lines

  1. /**
  2. *
  3. * Name        KBSTUFF -- Force a string into the BIOS type-ahead
  4. *               buffer.
  5. *
  6. * Synopsis    premain = kbstuff (where, pstring);
  7. *
  8. *        char *premain      Pointer to first character in string
  9. *                  NOT inserted in queue, or to the
  10. *                  trailing NUL ('\0') if they were all
  11. *                  placed in the queue.
  12. *
  13. *        int where      KB_HEAD if string to be placed at head
  14. *                  of queue, KB_TAIL if at tail.
  15. *
  16. *        char *pstring      Pointer to first character in string.
  17. *                  String must be terminated by NUL ('\0').
  18. *
  19. * Description    This function forces a string of keystrokes into the
  20. *        BIOS type-ahead queue as if they were typed by the
  21. *        operator.  The string may be inserted at the tail of
  22. *        the queue (as if most recently typed) or at the head
  23. *        (least recently typed).
  24. *
  25. *        If characters in the string are to be placed at the
  26. *        tail of the queue, then as many as possible are
  27. *        inserted and the value of the function is returned
  28. *        as a pointer to the first character NOT inserted.
  29. *
  30. *        If the string is to be placed at the head of the
  31. *        queue, then characters are inserted into the queue if
  32. *        and only if they all fit.  (Therefore no string
  33. *        longer than 15 characters may be placed at the head
  34. *        of the queue.)
  35. *
  36. *        The BIOS keyboard buffer is arranged as a circular
  37. *        queue of fifteen pairs of bytes (plus one pair for
  38. *        overhead).  For extended key sequences the character
  39. *        value is 0 and the scan code is as described in the
  40. *        IBM Technical Reference.  For ASCII characters, on the
  41. *        other hand, the first byte of the pair is the ASCII
  42. *        code for the character and the second is the
  43. *        keystroke's scan code (if the key was generated by a
  44. *        normal scan code) or 0 (if the key was generated by ALT
  45. *        plus the numeric ASCII code on the numeric keypad).
  46. *        For instance,
  47. *
  48. *            pressing 'A' generates character 65, scan code 30,
  49. *
  50. *        but
  51. *
  52. *            pressing ALT + 65 generates character 65, scan
  53. *            code 0.
  54. *
  55. *        (Virtually all application programs treat those two
  56. *        cases identically.)
  57. *
  58. *        For ordinary ASCII characters in the string, this
  59. *        function inserts 0 as the scan code.
  60. *
  61. *        To indicate extended key sequences in the string, use
  62. *        the character '\377' followed by the scan code.  This
  63. *        function will insert 0 as the character value followed
  64. *        by the scan code.
  65. *
  66. * Returns    premain       Pointer to first character in string
  67. *                  NOT inserted in queue, or to the
  68. *                  trailing NUL ('\0') if they were all
  69. *                  placed in the queue.
  70. *
  71. * Version    6.00 (C)Copyright Blaise Computing Inc.  1986-1989
  72. *
  73. **/
  74.  
  75.  
  76. #include <bkeybrd.h>
  77. #include <butil.h>
  78.  
  79. #define EXT_FLAG ((char) '\377') /* Indicates scan code of           */
  80.                  /* extended key sequence.         */
  81.  
  82. char *kbstuff(where,pstring)
  83. int  where;
  84. char *pstring;
  85. {
  86.     int       length,i;
  87.     register int  j;
  88.     register char *ptr;
  89.     char      *pend;
  90.     int       ints_were_on;
  91.     int       total_size;
  92.     char      value,scan;
  93.  
  94.     if (where == KB_TAIL)
  95.     {                    /* Stuff at tail of queue.  */
  96.     ints_were_on = utintoff();    /* Turn interrupts off.     */
  97.     for (; *pstring; pstring++)
  98.     {
  99.         if (*pstring == EXT_FLAG)
  100.         {
  101.         value = '\0';
  102.         scan  = *++pstring;
  103.         }
  104.         else
  105.         {
  106.         value = *pstring;
  107.         scan  = '\0';
  108.         }
  109.         if (kbplace(where,value,scan))    /* Try to stuff it. */
  110.         break;            /* Quit if it didn't fit.   */
  111.     }
  112.     }
  113.     else
  114.     {                    /* Stuff at head of queue.  */
  115.     length = 0;
  116.     for (ptr = pstring; *ptr; ptr++)
  117.     {                /* Measure length of string.*/
  118.         length++;
  119.         if (*ptr == EXT_FLAG)
  120.         ptr++;
  121.     }
  122.     pend = ptr;            /* Pointer to end of string.*/
  123.  
  124.     ints_were_on = utintoff();    /* Turn interrupts off.     */
  125.  
  126.     if (length <= kbqueue(&total_size))
  127.     {                /* The string fits.        */
  128.         for (i = length; i; i--)    /* Stuff each character in  */
  129.         {                /* reverse order.        */
  130.         ptr = pstring;
  131.         for (j = 1; j < i; j++)
  132.         {            /* Step past first (i-1)    */
  133.                     /* characters in string.    */
  134.             if (*ptr == EXT_FLAG)
  135.             ptr++;
  136.             ptr++;
  137.         }
  138.                     /* Now ptr points to i-th   */
  139.                     /* character in string.     */
  140.         if (*ptr == EXT_FLAG)
  141.             kbplace(where,
  142.                 (char) 0,
  143.                 *(ptr + 1));    /* Scan code only.  */
  144.         else
  145.             kbplace(where,
  146.                 *ptr,        /* Character only.  */
  147.                 (char) 0);
  148.         }
  149.         pstring = pend;        /* All characters stuffed.  */
  150.     }
  151.     else
  152.     {
  153.         /* Do nothing. */        /* String too big:  No        */
  154.     }                /* characters stuffed.        */
  155.     }
  156.  
  157.     if (ints_were_on)
  158.     utinton();        /* Turn interrupts back on.        */
  159.     return pstring;        /* Return remainder of string.        */
  160. }
  161.