home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / WINSPAWN.CMM < prev    next >
Text File  |  1995-02-03  |  5KB  |  164 lines

  1. //*****************************************************************
  2. //*** WinSpawn.cmm - Run from CEnvi for Windows to allow the    ***
  3. //*** ver.2          WinSpawn() routine in WinSpawn.lib to      ***
  4. //***                call the spawn command in Windows sessions ***
  5. //***                running under WINOS2.                      ***
  6. //*****************************************************************
  7.  
  8. if ( !defined(_WINDOWS_) ) {
  9.    printf("\a\nThis program must be executed from CEnvi for Windows\n");
  10.    printf("to serve the OS/2 WinSpawn() routine from CEnvi for OS/2.\n");
  11.    printf("Press any key to exit...");
  12.    getch();
  13.    printf("\n");
  14.    exit(EXIT_FAILURE);
  15. }
  16.  
  17. #include <OptParms.lib>
  18.  
  19. main(argc,argv)
  20. {
  21.    Hide = OptionalParameter(argc,argv,"HIDE");
  22.    FileName = argv[1];
  23.    WaitTime = atoi(argv[2]);
  24.  
  25.    if ( 3 != argc  ||  WaitTime < 1 ) {
  26.       Instructions();
  27.       return EXIT_FAILURE;
  28.    }
  29.  
  30.    // Give this session the name of the file
  31.    sprintf(NewTitle,"WinSpawn: %s",FileName);
  32.    DynamicLink("USER","SETWINDOWTEXT",SWORD16,PASCAL,ScreenHandle(),NewTitle);
  33.  
  34.    // If want to be hidden, then hide it
  35.    if ( Hide )
  36.       DynamicLink("USER","SHOWWINDOW",SWORD16,PASCAL,ScreenHandle(),0);
  37.  
  38.    SpawnForever(FileName,WaitTime);
  39.    return EXIT_SUCCESS;
  40. }
  41.  
  42. #define WIN_SPAWN       0  // spawn command and return immediately
  43. #define WIN_SPAWN_WAIT  1  // spawn and wait until command is finished
  44. #define WIN_SPAWN_EXIT  2  // spawn and then exit WinSpawn.cmm
  45.  
  46. SpawnForever(pFileSpec,pDelay)
  47.    // get all messages from WinSpawn.lib and spawn them
  48. {
  49.    sprintf(FullPipeSpec,"\\PIPE\\%s",pFileSpec);
  50.    lExit = False;
  51.    do {
  52.       suspend(pDelay);
  53.  
  54.       // try to read command from pipe file
  55.       if ( DosOpen(lHandle,FullPipeSpec,0xC2) ) {
  56.  
  57.          // read the action to perform, how many bytes in the message,
  58.          // and then the message
  59.          if ( DosReadUWORD32(lHandle,lAction)
  60.            && DosReadUWORD32(lHandle,lCommandLen,UWORD32)
  61.            && lCommandLen
  62.            && DosRead(lHandle,lCommand,lCommandLen) ) {
  63.  
  64.             // final check that Command is CommandLen long
  65.             if ( strlen(lCommand) == (lCommandLen-1) ) {
  66.  
  67.                // perform spawn command
  68.                if ( 1 == lCommandLen ) {
  69.                   lResult = 0;   // nothing happened
  70.                } else {
  71.                   lResult = spawn(WIN_SPAWN_WAIT == lAction ? P_WAIT : P_NOWAIT,
  72.                                   lCommand);
  73.                }
  74.  
  75.                // send back return code
  76.                DosRewind(lHandle);
  77.                BLObPut(lResultBLOb,0,lResult,SWORD32);
  78.                DosWrite(lHandle,lResultBLOb,BLObSize(lResultBLOb));
  79.  
  80.                if ( WIN_SPAWN_EXIT == lAction )
  81.                   lExit = True;
  82.             }
  83.          }
  84.          DosClose(lHandle);
  85.       }
  86.  
  87.    } while ( !lExit );
  88. }
  89.  
  90. DosOpen(pHandle,pFileName,pAttributes)
  91. {
  92.    lInReg.ds = segment(pFileName);
  93.    lInReg.dx = offset(pFileName);
  94.    lInReg.ah = 0x3D;
  95.    lInReg.al = pAttributes;
  96.    lSuccess = interrupt(0x21,lInReg,lRegOut);
  97.    pHandle = lRegOut.ax;
  98.    return lSuccess;
  99. }
  100.  
  101. DosClose(pHandle)
  102. {
  103.    lReg.ah = 0x3E;
  104.    lReg.bx = pHandle;
  105.    interrupt(0x21,lReg,lRegOut);
  106. }
  107.  
  108. DosWrite(pHandle,pBuffer,pBufferLen)
  109. {
  110.    lInReg.ah = 0x40;
  111.    lInReg.bx = pHandle;
  112.    lInReg.cx = pBufferLen;
  113.    lInReg.ds = segment(pBuffer);
  114.    lInReg.dx = offset(pBuffer);
  115.    return ( interrupt(0x21,lInReg,lOutReg) && lOutReg.ax == pBufferLen );
  116. }
  117.  
  118. DosRead(pHandle,pBuffer,pBufferLen)
  119. {
  120.    BLObSize(pBuffer,pBufferLen);
  121.    lInReg.ah = 0x3F;
  122.    lInReg.bx = pHandle;
  123.    lInReg.cx = pBufferLen;
  124.    lInReg.ds = segment(pBuffer);
  125.    lInReg.dx = offset(pBuffer);
  126.    return ( interrupt(0x21,lInReg,lOutReg) && lOutReg.ax == pBufferLen );
  127. }
  128.  
  129. DosReadUWORD32(pHandle,pUWORD32)
  130. {
  131.    if ( !DosRead(pHandle,lBuf,4) )
  132.       return False;
  133.    pUWORD32 = BLObGet(lBuf,0,UWORD32);
  134.    return True;
  135. }
  136.  
  137. DosRewind(pHandle)
  138. {
  139.    lReg.ax = 0x42;
  140.    lReg.al = 0;
  141.    lReg.cx = lReg.dx = 0;
  142.    interrupt(0x21,lReg);
  143. }
  144.  
  145. Instructions()
  146. {
  147.    puts("\a");
  148.    puts(`WinSpawn - Perform the CEnvi spawn() function from WINOS2`);
  149.    puts(``)
  150.    puts(`SYNTAX: CENVI2 WINSPAWN <PipeSpec> <Delay> [/HIDE]`)
  151.    puts(``)
  152.    puts(`WHERE: PipeSpec - Unique 8.3 name to identify this WINSPAWN session`)
  153.    puts(`       Delay - time (milliseconds) to delay between checks`)
  154.    puts(`       HIDE - Optional; hide thise CEnvi program icon`)
  155.    puts(``)
  156.    puts(`Examples: the following examples run from OS/2`)
  157.    puts(`  START /FS /F C:\CENVIWIN\CENVIW.EXE D:\CENVIOS2\WinSpawn FAXER 3000`)
  158.    puts(`  START /WIN C:\CENVIWIN\CENVIW.EXE FAXER 5000 /HIDE`)
  159.    puts(``)
  160.    puts(`Press any key to exit...`)
  161.    getch()
  162. }
  163.  
  164.