home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name kbstuff -- Force a string into the BIOS type-ahead
- * buffer.
- *
- * Synopsis premainder = kbstuff(at_head,pstring);
- *
- * char *premainder Pointer to first character in string
- * NOT inserted in queue, or to the
- * trailing NUL ('\0') if they were all
- * placed in the queue.
- * int at_head KB_HEAD if string to be placed at head
- * of queue, KB_TAIL if at tail.
- * char *pstring Pointer to first character in string.
- * String must be terminated by NUL ('\0').
- *
- * Description This function forces a string of keystrokes into the
- * BIOS type-ahead queue as if they were typed by the
- * operator. The string may be inserted at the tail of the
- * queue (as if most recently typed) or at the head (least
- * recently typed).
- *
- * If characters in the string are to be placed at the tail
- * of the queue, then as many as possible are inserted and
- * the value of the function is returned as a pointer to
- * the first character NOT inserted.
- *
- * If the string is to be placed at the head of the queue,
- * then characters are inserted into the queue if and only
- * if they all fit. (Therefore no string longer than 15
- * characters may be placed at the head of the queue.)
- *
- * The BIOS keyboard buffer is arranged as a circular queue
- * of fifteen pairs of bytes (plus one pair for overhead).
- * For extended key sequences the character value is 0 and
- * the scan code is as described in the IBM Technical
- * Reference. For ASCII characters, on the other hand, the
- * first byte of the pair is the ASCII code for the
- * character and the second is the keystroke's scan code
- * (if the key was generated by a normal scan code) or 0
- * (if the key was generated by ALT plus the numeric ASCII
- * code on the numeric keypad). For instance,
- *
- * pressing 'a' generates character 65, scan code 30,
- *
- * but
- *
- * pressing ALT + 65 generates character 65, scan code 0.
- *
- * (Virtually all application programs treat those two
- * cases identically.)
- *
- * For ordinary ASCII characters in the string, this
- * function inserts 0 as the scan code.
- *
- * To indicate extended key sequences in the string, use
- * the character '\377' followed by the scan code. This
- * function will insert 0 as the character value followed
- * by the scan code.
- *
- * Returns premainder Pointer to first character in string
- * NOT inserted in queue, or to the
- * trailing NUL ('\0') if they were all
- * placed in the queue.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- **/
-
- #include <bkeybd.h>
- #include <butility.h>
-
- #define EXT_FLAG '\377' /* Indicates scan code of */
- /* extended key sequence. */
-
- char *kbstuff(at_head,pstring)
- int at_head;
- char *pstring;
- {
- int length,i;
- register int j;
- register char *ptr;
- char *pend;
- int ints_were_on;
- int total_size;
- char value,scan;
-
- if (at_head == KB_TAIL)
- { /* Stuff at tail of queue. */
- ints_were_on = utintoff(); /* Turn interrupts off. */
- for (; *pstring; pstring++)
- {
- if (*pstring == EXT_FLAG)
- {
- value = '\0';
- scan = *++pstring;
- }
- else
- {
- value = *pstring;
- scan = '\0';
- }
- if (kbplace(at_head,value,scan)) /* Try to stuff it. */
- break; /* Quit if it didn't fit. */
- }
- }
- else
- { /* Stuff at head of queue. */
- length = 0;
- for (ptr = pstring; *ptr; ptr++)
- { /* Measure length of string. */
- length++;
- if (*ptr == EXT_FLAG)
- ptr++;
- }
- pend = ptr; /* Pointer to end of string. */
-
- ints_were_on = utintoff(); /* Turn interrupts off. */
-
- if (length <= kbqueue(&total_size))
- { /* The string fits. */
- for (i = length; i; i--) /* Stuff each character in */
- { /* reverse order. */
- ptr = pstring;
- for (j = 1; j < i; j++)
- { /* Step past first (i-1) */
- /* characters in string. */
- if (*ptr == EXT_FLAG)
- ptr++;
- ptr++;
- }
- /* Now ptr points to i-th */
- /* character in string. */
- if (*ptr == EXT_FLAG)
- kbplace(at_head,
- (char) 0,
- *(ptr + 1)); /* Scan code only. */
- else
- kbplace(at_head,
- *ptr, /* Character only. */
- (char) 0);
- }
- pstring = pend; /* All characters stuffed. */
- }
- else
- {
- /* Do nothing. */ /* String too big: No */
- } /* characters stuffed. */
- }
-
- if (ints_were_on)
- utinton(); /* Turn interrupts back on. */
- return pstring; /* Return remainder of string. */
- }