home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / slipfree.zip / SlipFree.cpp < prev    next >
C/C++ Source or Header  |  1994-11-29  |  6KB  |  220 lines

  1. /*
  2. ** SlipFree.cpp
  3. **
  4. ** Requests access to the com port from SLIP.
  5. **
  6. ** Mike Lempriere <mikel@networx.com> 22-Nov-94
  7. **
  8. ** Last mod 29-Nov-94
  9. **
  10. ** This program would not have been possible without the help of
  11. ** the original IBM TCP/IP SLIP author, David Bolen <db3l@ans.net>.
  12. */
  13.  
  14. /*
  15.       - - - - - - - - - - - - - - - - - - - - - - - - -
  16.  
  17. SLIP Utility Program Access to IBM OS/2 Slip Driver v1.0  -- David Bolen
  18.  
  19. The following should be done by the program wanting to tell the SLIP
  20. driver it would like access to the COM port:
  21.  
  22.      Initialization
  23.     - Check if the SLIP driver is running by opening the system
  24.       semaphore \sem32\slip\monitor.
  25.     - Open the following three system semaphores:
  26.         \sem32\slip\pause        (com_pause)
  27.         \sem32\slip\paused        (com_paused)
  28.         \sem32\slip\continue        (com_continue)
  29.  
  30.      COM Port Request
  31.     - Post com_pause
  32.     - Wait for com_paused
  33.  
  34.      --- Access COM port ---
  35.  
  36.      COM Port Release
  37.     - Post com_continue
  38.  
  39.  
  40. The above should not be considered a "published" procedure or API -
  41. but at the same time, it's what is part of v1.0.  It will likely
  42. change in subsequent versions when the SLIP driver starts supporting
  43. multiple interfaces, and provides a more extensive functional
  44. interface for external utilities.
  45.  
  46. Once the SLIP driver posts com_paused, although the driver still has a
  47. shared open on the port, it will not issue any I/O requests to the
  48. port until the com_continue semaphore is posted.  Thus, any other
  49. utility (including the one that requested the pause as is the case
  50. with SLIPTERM) can access the port as long as they issue the DosOpen
  51. in sharing mode (OPEN_SHARE_DENYNONE).
  52.  
  53.       - - - - - - - - - - - - - - - - - - - - - - - - -
  54. */
  55.  
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. #define INCL_DOS
  62. #define INCL_DOSDEVIOCTL
  63. #include <os2.h>
  64.  
  65. const char * Version = "ver 1.0, 29-Nov-94";
  66.  
  67. // This stupid thing is because Borland's BSEDOS.H is not
  68. // properly CONST'ed, so we must cast away the CONST'ness.
  69. #ifdef __BORLANDC__
  70. #define BORCAST (PSZ)
  71. #else
  72. #define BORCAST
  73. #endif
  74.  
  75. static int Debug = 0;
  76. static short Timeout = 0;
  77. static char Command[1024];
  78.  
  79. static HEV com_pause;
  80. static HEV com_paused;
  81. static HEV com_continue;
  82. static HEV slip_running;
  83.  
  84.  
  85. static void Usage(void) {
  86.     fprintf(stderr, "SlipFree %s - Mike Lempriere (mikel@networx.com)\n", Version);
  87.     fprintf(stderr, "\nAsks SLIP for COM port access, starts given program,\n");
  88.     fprintf(stderr, "and returns COM port control back to SLIP when that program completes.\n");
  89.     fprintf(stderr, "\nUsage: SLIPFREE [switches] command [command args]\n");
  90.     fprintf(stderr, "SWITCHES:\n");
  91.     fprintf(stderr, "-?, -h\tThis help screen.\n");
  92.     fprintf(stderr, "-w#\tWait up to '#' seconds for SLIP to start.\n");
  93.     fprintf(stderr, "\n'command' will be executed as a command line,\n");
  94.     fprintf(stderr, "with any remaining args passed as it's args.\n");
  95.     fprintf(stderr, "\nEXAMPLES:\n");
  96.     fprintf(stderr, "    SLIPFREE commn\n");
  97.     fprintf(stderr, "        runs 'commn' without your having to shutdown or restart slip.\n");
  98.     exit(1);
  99. }                        // Usage()
  100.  
  101.  
  102. static void WaitForSlip(void) {
  103.  
  104.     int FirstTime = 1;
  105.     APIRET rc;
  106.     short Seconds = Timeout;
  107.     do {
  108.         rc = DosOpenEventSem("\\sem32\\slip\\monitor", &slip_running);
  109.         if (rc) {
  110.             if (FirstTime) {
  111.                 printf("[ Waiting for SLIP - maximum wait = %d seconds ]\n",
  112.                        Timeout);
  113.                 FirstTime = 0;
  114.             }
  115.             Seconds--;
  116.             if (Seconds >= 0) {
  117.                 if (Debug) {
  118.                     fprintf(stderr, ".");
  119.                     fflush(stderr);
  120.                 }
  121.                 DosSleep(1000L);
  122.             }
  123.         }
  124.     } while (rc && (Seconds >= 0));
  125.     if (Debug) {
  126.         fprintf(stderr, "\n");
  127.     }
  128.     if (Seconds < 0) {
  129.         printf("The SLIP driver appears not to have started.\n");
  130.         exit(1);
  131.     }
  132. }                        // WaitForSlip()
  133.  
  134.  
  135. static void OpenASem(const char *Name, HEV * Hev) {
  136.  
  137.     APIRET rc = DosOpenEventSem(BORCAST Name, Hev);
  138.     if (rc != 0) {
  139.         fprintf(stderr, "Error %lu accessing %s - is SLIP up?\n", rc, Name);
  140.         exit(1);
  141.     }
  142. }                        // OpenASem()
  143.  
  144.  
  145. static void ParseArgs(int argc, const char * const * argv) {
  146.  
  147.     int MoreSwitches;
  148.     for (int argi = 1; argi < argc; argi++) {
  149.         MoreSwitches = (argv[argi][0] == '-');
  150.         if (MoreSwitches) {
  151.             char Ch = (argv[argi][1]);
  152.             switch (Ch) {
  153.             case 'd':
  154.                 Debug = (argv[argi][2] - '0');
  155.                 if (Debug < 1) {
  156.                     Debug = TRUE;
  157.                 }
  158.                 break;
  159.             case 'w':
  160.                 Timeout = (short) atoi(&argv[argi][2]);
  161.                 if (Timeout < 1) {
  162.                     Timeout = 30;
  163.                 }
  164.                 break;
  165.             case '?':
  166.             case 'h':
  167.                 Usage();
  168.                 break;
  169.             default:
  170.                 fprintf(stderr, "Unrecognized command line switch %d\n", Ch);
  171.                 Usage();
  172.             }
  173.         }
  174.         else {
  175.             if (*Command) {
  176.                 strcat(Command, " ");
  177.             }
  178.             strcat(Command, argv[argi]);
  179.         }
  180.     }
  181. }                        // ParseArgs()
  182.  
  183.  
  184. int main(int argc, const char * const * argv) {
  185.  
  186.     ParseArgs(argc, argv);
  187.  
  188.     if (!(*Command)) {
  189.         Usage();
  190.     }
  191.  
  192.     if (Timeout) {
  193.         WaitForSlip();
  194.     }
  195.  
  196.     OpenASem("\\sem32\\slip\\com\\pause", &com_pause);
  197.     OpenASem("\\sem32\\slip\\com\\paused", &com_paused);
  198.     OpenASem("\\sem32\\slip\\com\\continue", &com_continue);
  199.  
  200.     printf("[ Requesting COM port access from SLIP driver ]\n");
  201.     DosPostEventSem(com_pause);
  202.  
  203.     // Wait for SLIP to let us have it.
  204.     DosWaitEventSem(com_paused, SEM_INDEFINITE_WAIT);
  205.  
  206.     // Let them know they got it.
  207.     printf("[ SLIP driver granted access ]\n");
  208.  
  209.     // Perform the command.
  210.     printf("starting %s\n", Command);
  211.     int Err = system(Command);
  212.     if (Err != 0) {
  213.         fprintf(stderr, "system err=%d\n", Err);
  214.     }
  215.  
  216.     // And lastly - notify SLIP driver we are done.
  217.     DosPostEventSem(com_continue);
  218.     return 0;
  219. }                        // main()
  220.