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

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <dos.h>
  6.  
  7. #if !defined(__ZTC__) && !defined(__TURBOC__)
  8.  #define MK_FP(seg,offset) \
  9.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  10.  #define peek(s,o) (*(MK_FP(s,o)))
  11.  #define poke(s,o,w) (*(MK_FP(s,o)))=(w)
  12. #endif
  13.  
  14. #define SUCCESS 0
  15. #define ERROR -1
  16.  
  17. static unsigned head, tail;
  18. static int idx = 0;
  19. static unsigned keystack[16][2];
  20.  
  21. /****************************************************************/
  22. /*                                                              */
  23. /*  ungetkey()                                                  */
  24. /*                                                              */
  25. /*  Stuffs characters into the keyboard buffer.                 */
  26. /*                                                              */
  27. /*  Parameters: 1 - Extended character to stuff                 */
  28. /*                                                              */
  29. /*  Returns: SUCCESS or EOF                                     */
  30. /*                                                              */
  31. /*  Note: This function assumes that the keyboard buffer is in  */
  32. /*        the normal (for IBM) location of 40:1E.               */
  33. /*                                                              */
  34. /****************************************************************/
  35.  
  36. int ungetkey(unsigned key)
  37. {
  38.         int count;
  39.  
  40. #ifdef __ZTC__
  41.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  42.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  43. #else
  44.         head = peek(0x40, 0x1a);
  45.         tail = peek(0x40, 0x1c);
  46. #endif
  47.         count = tail - head;
  48.         if (0 > count)
  49.                 count += (16 * sizeof(unsigned));
  50.         count >>= 1;
  51.  
  52.         if (15 > count)
  53.         {
  54. #ifdef __ZTC__
  55.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  56. #else
  57.                 keystack[idx][0] = peek(0x40, tail);
  58. #endif
  59.                 keystack[idx][1] = tail;
  60. #ifdef __ZTC__
  61.                 poke(0x40, tail, &key, sizeof(unsigned));
  62. #else
  63.                 poke(0x40, tail, key);
  64. #endif
  65.                 tail += sizeof(unsigned);
  66.                 if (0x3e <= tail)
  67.                         tail = 0x1e;
  68. #ifdef __ZTC__
  69.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  70. #else
  71.                 poke(0x40, 0x1c, tail);
  72. #endif
  73.                 return key;
  74.         }
  75.         return EOF;
  76. }
  77.  
  78. /****************************************************************/
  79. /*                                                              */
  80. /*  KB_stuff()                                                  */
  81. /*                                                              */
  82. /*  Stuffs strings into the keyboard buffer.                    */
  83. /*                                                              */
  84. /*  Parameters: 1 - String to stuff                             */
  85. /*                                                              */
  86. /*  Returns: SUCCESS if successful                              */
  87. /*           ERROR   in case of error, plus beyboard buffer is  */
  88. /*                   restored                                   */
  89. /*                                                              */
  90. /*  Note: This function assumes that the keyboard buffer is in  */
  91. /*        the normal (for IBM) location of 40:1E.               */
  92. /*                                                              */
  93. /****************************************************************/
  94.  
  95. int KB_stuff(char *str)
  96. {
  97.         int ercode = SUCCESS;
  98.  
  99.         idx = 0;
  100.         while (*str)
  101.         {
  102.                 if (EOF == ungetkey((unsigned)(*str++)))
  103.                 {
  104.                         while (0 <= --idx)
  105.                         {
  106.                                 tail = keystack[idx][1];
  107. #ifdef __ZTC__
  108.                                 poke(0x40, tail, &keystack[idx][0],
  109.                                         sizeof(unsigned));
  110. #else
  111.                                 poke(0x40, tail, keystack[idx][0]);
  112. #endif
  113.                         }
  114. #ifdef __ZTC__
  115.                         poke(0x40, 0x1c, &tail, sizeof(unsigned));
  116. #else
  117.                         poke(0x40, 0x1c, tail);
  118. #endif
  119.                         ercode = ERROR;
  120.                         break;
  121.                 }
  122.                 else    ++idx;
  123.         }
  124.         idx = 0;
  125.         return ercode;
  126. }
  127.  
  128. main(int argc, char *argv[])
  129. {
  130.         FILE *bfile;
  131.  
  132.         if (3 > argc)
  133.         {
  134.                 puts("\aUsage: SETENVAR envar datum");
  135.                 abort();
  136.         }
  137.         bfile = fopen("$TMP$.BAT", "w");
  138.         fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\n", argv[1], argv[2]);
  139.         fclose(bfile);
  140.         while (kbhit())
  141.                 ;
  142.         KB_stuff("$tmp$\r");
  143. }
  144.