home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / ipxlib / input.c next >
Encoding:
C/C++ Source or Header  |  1994-06-01  |  11.7 KB  |  241 lines

  1. // INPUT.C -- Input routines for IPXTEST
  2. // by Allen Brunson  06/01/94
  3.  
  4.  
  5. #include <conio.h>     // kbhit()
  6. #include <string.h>    // strcmp(), strcpy(), strlen(), strupr()
  7. #ifdef _MSC_VER        // If a Microsoft compiler
  8. #include <graph.h>     // settextcursor(), settextposition()
  9. #endif
  10. #include "ipxtest.h"   // IPXtest-specific defines
  11.  
  12.  
  13. /****************************************************************************/
  14. /*                                                                          */
  15. /***  Global data                                                         ***/
  16. /*                                                                          */
  17. /****************************************************************************/
  18.  
  19. char cmdStr[MAXCOLS];                              // Command string
  20. byte inputFlag = FALSE;                            // End of input flag
  21.  
  22. struct COMMAND cmd[CMDTOTAL] =                     // Command strings
  23.   {
  24.     {"B",         cBROADCAST},                     // Broadcast
  25.     {"BROADCAST", cBROADCAST},                     // Broadcast
  26.     {"CLS",       cCLS},                           // Clear screen
  27.     {"D",         cDISPLAY},                       // Display users
  28.     {"DISPLAY",   cDISPLAY},                       // Display users
  29.     {"F",         cFLURRY},                        // Flurry mode
  30.     {"FLURRY",    cFLURRY},                        // Flurry mode
  31.     {"H",         cHELP},                          // Help
  32.     {"HELP",      cHELP},                          // Help
  33.     {"M",         cMESSAGE},                       // Message
  34.     {"MESSAGE",   cMESSAGE},                       // Message
  35.     {"N",         cNAME},                          // Name
  36.     {"NAME",      cNAME},                          // Name
  37.     {"P",         cPING},                          // Ping packet
  38.     {"PING",      cPING},                          // Ping packet
  39.     {"S",         cSTAT},                          // Statistics
  40.     {"STAT",      cSTAT},                          // Statistics
  41.     {"Q",         cQUIT},                          // Quit
  42.     {"QUIT",      cQUIT},                          // Quit
  43.   };
  44.  
  45.  
  46. /****************************************************************************/
  47. /*                                                                          */
  48. /***  cmdHelp()                                                           ***/
  49. /*                                                                          */
  50. /****************************************************************************
  51.  
  52. This procedure displays a list of user commands.                            */
  53.  
  54. void cmdHelp(void)                                 // Begin cmdHelp()
  55.   {
  56.     message("");
  57.     message("Commands:");
  58.     message("BROADCAST, B [message]          Broadcast message to all users");
  59.     message("CLS                             Clear the screen");
  60.     message("DISPLAY, D                      Show known users");
  61.     message("FLURRY, F [ON | OFF | RESET]    Toggle flurry sends, reset count");
  62.     message("HELP, H                         Print this message");
  63.     message("MESSAGE, M [usernum] [message]  Send message to one user");
  64.     message("NAME, N [name]                  Set your name");
  65.     message("PING, P                         Update known users");
  66.     message("STAT, S [RESET]                 Display or reset ECB statistics");
  67.     message("QUIT, Q                         End the program");
  68.     message("");
  69.     message("The text of a received message is interpreted as a command.");
  70.     message("");
  71.     message("Keys:");
  72.     message("F1                              Same as HELP");
  73.     message("F3                              Recall last command line");
  74.     message("Esc                             Clear command line");
  75.     message("");
  76.   }                                                // End cmdHelp()
  77.  
  78.  
  79. /****************************************************************************/
  80. /*                                                                          */
  81. /***  cmdProcess()                                                        ***/
  82. /*                                                                          */
  83. /****************************************************************************
  84.  
  85. This procedure processes commands entered by the user.                      */
  86.  
  87. void cmdProcess(void)                              // Begin cmdProcess()
  88.   {
  89.     word i, j;                                     // Loop variables
  90.     byte iFlag;                                    // Copy of input flag
  91.  
  92.     if (inputFlag == FALSE) return;                // Return if no input
  93.  
  94.     iFlag = inputFlag;                             // Save input flag
  95.     inputFlag = FALSE;                             // Clear input flag
  96.  
  97.     if (strlen(cmdStr) == 0) return;               // Return if zero-length
  98.  
  99.     j = 0; while (cmdStr[j] == ' ') j++;           // Go past spaces
  100.  
  101.     for (i = 0; cmdStr[j] != 0 &&                  // Copy first word from
  102.      cmdStr[j] != ' '; i++, j++)                   //  cmdStr to str
  103.       str[i] = cmdStr[j];
  104.  
  105.     str[i] = 0; strupr(str);                       // str to uppercase
  106.  
  107.     for (i = 0; i < CMDTOTAL; i++)                 // Loop for all commands
  108.       if (!strcmp(str, cmd[i].str))                // If a match is found
  109.         switch (cmd[i].code)                       // Decision on code
  110.           {
  111.             case cBROADCAST:                       // Broadcast
  112.               if (iFlag == 2) return;              // Ignore remote requests
  113.               sendBroadcast();                     // Call sendBroadcast()
  114.               return;
  115.  
  116.             case cCLS:                             // CLS
  117.               vidCLS();                            // Call vidCLS()
  118.               return;
  119.  
  120.             case cDISPLAY:                         // Display
  121.               userDisplay();                       // Display users
  122.               return;
  123.  
  124.             case cFLURRY:                          // Flurry
  125.               sendFlurryMode();                    // Set flurry mode
  126.               return;
  127.  
  128.             case cHELP:                            // Help
  129.               cmdHelp();                           // Display command help
  130.               return;
  131.  
  132.             case cMESSAGE:                         // Message
  133.               if (iFlag == 2) return;              // Ignore remote requests
  134.               sendMessage();                       // Call sendMessage()
  135.               return;
  136.  
  137.             case cNAME:                            // Name
  138.               setName();                           // Set user name
  139.               return;
  140.  
  141.             case cPING:                            // Ping
  142.               sendPing();                          // Send ping packet
  143.               return;
  144.  
  145.             case cSTAT:                            // Statistics
  146.               statDisplay();                       // Display statistics
  147.               return;
  148.  
  149.             case cQUIT:                            // Quit
  150.               endProgram = TRUE;                   // Set endProgram flag
  151.               return;
  152.           }
  153.  
  154.     if (iFlag == 1)                                // If not a remote command
  155.       {
  156.         message("");
  157.         message("Unrecognized command.  "          // Display this message
  158.          "Press F1 for list.");                    //  if no match found
  159.         message("");
  160.       }
  161.   }                                                // End cmdProcess()
  162.  
  163.  
  164. /****************************************************************************/
  165. /*                                                                          */
  166. /***  getKeys()                                                           ***/
  167. /*                                                                          */
  168. /****************************************************************************
  169.  
  170. This procedure processes one key of user input at a time.  When Enter is
  171. pressed, it copies the command string to cmdStr.                            */
  172.  
  173. void getKeys(void)                                 // Begin getKeys()
  174.   {
  175.     static char inputStr[MAXCOLS];                 // Input string
  176.     static char inputStrOld[MAXCOLS];              // Previous string
  177.     static char inputLen;                          // Input length
  178.  
  179.     word key;                                      // Key value
  180.  
  181.     if (!kbhit()) return;                          // Return if no keys
  182.  
  183.     key = getch();                                 // Get the key
  184.     if (!key) key = 0x7500 + getch();              // Get "special" keys
  185.  
  186.     if (key == 0x754D) key = (word) ' ';           // Right arrow to space
  187.  
  188.     switch(key)                                    // Decision on key
  189.       {
  190.         case 0x753B:                               // F1 key
  191.           strcpy(cmdStr, "H");                     // Set "help" string
  192.           inputFlag = TRUE;                        // Input is ready
  193.           return;
  194.  
  195.         case 0x753D:                               // F3 key
  196.           strcpy(inputStr, inputStrOld);           // Copy old inputStr
  197.           inputLen = strlen(inputStr);             // Set new input length
  198.           iCol = inputLen + 2;                     // Set new column
  199.           cursorOff();                             // Turn off cursor
  200.           gotoxy(2, iRow);                         // Go to line start
  201.           cprintf(inputStr);                       // Print string
  202.           gotoxy(iCol, iRow);                      // Update cursor pos
  203.           cursorOn();                              // Turn on cursor
  204.           return;
  205.  
  206.         case 0x000D:                               // Enter (fall thru to ESC)
  207.           strcpy(cmdStr, inputStr);                // Copy string to cmdStr
  208.           strcpy(inputStrOld, inputStr);           // Save for F3 use
  209.           inputFlag = TRUE;                        // Input is ready
  210.  
  211.         case 0x001B:                               // Esc
  212.           iCol = 2;                                // Go to left column
  213.           cursorOff();                             // Turn off cursor
  214.           gotoxy(iCol, iRow);                      // Cursor to left column
  215.           cprintf(strBlank);                       // Print blank string
  216.           gotoxy(iCol, iRow);                      // Cursor to left column
  217.           cursorOn();                              // Turn on cursor
  218.           inputStr[0] = 0;                         // Clear out inputStr
  219.           inputLen = 0;                            // Clear input length
  220.           return;
  221.  
  222.         case 0x0008: case 0x754B:                  // Backspace, left arrow
  223.           if (iCol <= 2) return;                   // Return if at left side
  224.           iCol--;                                  // Go back one column
  225.           gotoxy(iCol, iRow);                      // Position cursor
  226.           putch(' ');                              // Destroy old character
  227.           gotoxy(iCol, iRow);                      // Position cursor
  228.           inputLen--;                              // Subtract one key
  229.           inputStr[inputLen] = 0;                  // Remove it from string
  230.           return;
  231.  
  232.         default:                                   // Any other key
  233.           if (key < 32 || key > 254) return;       // Throw away non-ASCIIs
  234.           if (iCol >= cols - 1) return;            // Return if too far right
  235.           putch(key); iCol++;                      // Put key on screen
  236.           inputStr[inputLen] = (char) key;         // Put key in string
  237.           inputLen++; inputStr[inputLen] = 0;      // Terminate string
  238.           return;
  239.       }                                            // End switch()
  240.   }                                                // End getKeys()
  241.