home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / KBSTUFF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  4.7 KB  |  155 lines

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