home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DROPMANY.CMD < prev    next >
OS/2 REXX Batch file  |  1994-11-15  |  6KB  |  169 lines

  1. EXTPROC CEnvi2
  2. //***********************************************************
  3. //*** DropMany - Drop Many elements to execute code. This ***
  4. //*** ver.2      allows you to drag multiple filenames to ***
  5. //***            a program, and have them all passed to a ***
  6. //***            single instance of the program.          ***
  7. //***********************************************************
  8.  
  9. #include <GiveMem.lib>
  10.  
  11. main(argc,argv)
  12. {
  13.    if ( argc < 4  ||  (MaxWait = atoi(argv[1])) < 1 ) {
  14.       Instructions();
  15.       exit(EXIT_FAILURE);
  16.    }
  17.  
  18.    // name to drop in queue is the last argument
  19.    DropName = argv[argc-1];
  20.    QueueName = GetQueueName(argv[1]);
  21.  
  22.    // Create this queue, only first one to try will succeed
  23.    if ( QueueHandle = CreateQueue(QueueName) ) {
  24.       ICreatedQueue = True;
  25.       QueueProcess = Info().Process;
  26.    } else {
  27.       ICreatedQueue = False;
  28.       if ( !(QueueHandle = OpenQueue(QueueName,QueueProcess)) ) {
  29.          printf("\nUnable to open Queue \"%s\"\n",QueueName);
  30.          exit(EXIT_FAILURE);
  31.       }
  32.    }
  33.    if ( AddNameToQueue(QueueHandle,QueueProcess,DropName) ) {
  34.       if ( ICreatedQueue )
  35.          QueueEntries = GetQueueEntries(QueueHandle,MaxWait);
  36.    }
  37.    CloseQueue(QueueHandle);
  38.  
  39.    if ( ICreatedQueue ) {
  40.       if ( QueueEntries ) {
  41.          // create full statement to execute
  42.          strcpy(ExecuteCommand,defined(COMSPEC) ? COMSPEC : "CMD.EXE");
  43.          strcat(ExecuteCommand," /C");
  44.          for ( arg = 3; arg < argc; arg++ ) {
  45.             strcat(ExecuteCommand," ");
  46.             strcat(ExecuteCommand,argv[arg-1]);
  47.          }
  48.          for ( arg = GetArraySpan(QueueEntries); 0 <= arg; arg-- ) {
  49.             strcat(ExecuteCommand," ");
  50.             strcat(ExecuteCommand,QueueEntries[arg]);
  51.          }
  52.          printf("Execute: %s...",ExecuteCommand);
  53.          spawn(P_NOWAIT,ExecuteCommand);
  54.          printf("\n");
  55.       }
  56.    }
  57. }
  58.  
  59. Instructions()
  60. {
  61.    printf("\a\n")
  62.    printf("DropMany - Drop multiple files on single program instance\n")
  63.    printf("\n")
  64.    printf("USAGE: DROPMANY <Timeout> <Commands...> <FileSpec>\n")
  65.    printf("\n")
  66.    printf("WHERE: Timeout - time in milliseconds to wait for all files to tell that they\n")
  67.    printf("                 where \"dropped\" on this object.  Recommend about 6000 if\n")
  68.    printf("                 this is DropMany.cmd, or about 1000 if this has been turned\n")
  69.    printf("                 into an executable\n")
  70.    printf("       Commands - any commands to be performed on FileSpec\n")
  71.    printf("       FileSpec - Filename to be passed to Commands\n")
  72.    printf("\n")
  73.    printf("EXAMPLE: EPM.EXE takes multiple file specs. To drag lots of files to a program\n")
  74.    printf("         object and have them all started in a single instance of EPM.EXE,\n")
  75.    printf("         create program for DROPMANY, and add \"1200 START EPM.EXE\" as\n")
  76.    printf("         parameters.\n")
  77.    printf("\n")
  78. }
  79.  
  80. GetQueueName(pCommand)  // get a queue name based on given command
  81. {
  82.    lSplitName = SplitFileName(pCommand);
  83.    sprintf(lQueueName,"\\QUEUES\\%s%s",lSplitName.name,lSplitName.ext);
  84.    return lQueueName;
  85. }
  86.  
  87. CreateQueue(pQueueName) // return handle or NULL if cannot open
  88. {
  89.    #define QUEUE_FIFO   0
  90.    #define QUEUE_LIFO   1
  91.    #define ORD_DOS32CREATEQUEUE  16
  92.    return DynamicLink("QUECALLS",ORD_DOS32CREATEQUEUE,BIT32,CDECL,
  93.                       lQueueHandle,QUEUE_LIFO,pQueueName) ? NULL : lQueueHandle ;
  94. }
  95.  
  96. OpenQueue(pQueueName,pQueueProcess) // return handle or NULL if cannot open
  97. {
  98.    #define ORD_DOS32OPENQUEUE    15
  99.    rc = DynamicLink("QUECALLS",ORD_DOS32OPENQUEUE,BIT32,CDECL,
  100.                     lQueueProcess,lQueueHandle,pQueueName);
  101.    pQueueProcess = lQueueProcess;
  102.    return rc ? NULL : lQueueHandle ;
  103. }
  104.  
  105. CloseQueue(pQueueHandle)
  106. {
  107.    #define ORD_DOS32CLOSEQUEUE  11
  108.    return DynamicLink("QUECALLS",ORD_DOS32CLOSEQUEUE,BIT32,CDECL,pQueueHandle);
  109. }
  110.  
  111. AddNameToQueue(pQueueHandle,pQueueProcess,pName)
  112. {
  113.    // give shared memory to that process
  114.    lName = GiveMemoryToProcess(pQueueProcess);
  115.    poke(lName,pName,1+strlen(pName));
  116.    #define ORD_DOS32WRITEQUEUE   14
  117.    if ( DynamicLink("QUECALLS",ORD_DOS32WRITEQUEUE,BIT32,CDECL,
  118.                     pQueueHandle,0,1+strlen(pName),lName,0) ) {
  119.       printf("\nError adding name to queue\n");
  120.       return False;
  121.    }
  122.    return True;
  123. }
  124.  
  125. GetQueueEntries(pQueueHandle,pMaxWait)
  126. {
  127.    // Wait for queue to stop getting new entries
  128.    #define RETRIES_PER_INTERVAL  3
  129.    lQueueCount = 0;
  130.    for ( lKeepTrying = 0; lKeepTrying < RETRIES_PER_INTERVAL; lKeepTrying++ ) {
  131.       #define ORD_DOS32QUERYQUEUE   12
  132.       undefine(lNewQueueCount);
  133.       if ( DynamicLink("QUECALLS",ORD_DOS32QUERYQUEUE,BIT32,CDECL,
  134.                        pQueueHandle,lNewQueueCount) ) {
  135.          printf("\nError on DosQueryQueue\n");
  136.          return NULL;
  137.       }
  138.       if ( lNewQueueCount == lQueueCount ) {
  139.          suspend( pMaxWait / RETRIES_PER_INTERVAL );
  140.       } else {
  141.          lKeepTrying = -1;
  142.          lQueueCount = lNewQueueCount;
  143.       }
  144.    }
  145.    if ( !lQueueCount ) {
  146.       printf("\nNo entries found in queue\n");
  147.       return NULL;
  148.    }
  149.  
  150.    // read entries from queue
  151.    BLObSize(lRequest,2*4);
  152.    for ( lIdx = 0; lIdx < lQueueCount; lIdx++ ) {
  153.       #define ORD_DOS32READQUEUE 9
  154.       #define DCWW_WAIT          0
  155.       #define DCWW_NOWAIT        1
  156.       undefine(lDataLength); undefine(lDataPtr); undefine(lPriority);
  157.       BLObPut(lRequest,0,Info().Process,UWORD32);
  158.       if ( DynamicLink("QUECALLS",ORD_DOS32READQUEUE,BIT32,CDECL,
  159.                        pQueueHandle,lRequest,lDataLength,lDataPtr,
  160.                        0,DCWW_WAIT,lPriority,0) ) {
  161.          printf("\nError on DosReadQueue()\n");
  162.          return NULL;
  163.       }
  164.       lQueue[lIdx] = peek(lDataPtr,lDataLength);
  165.    }
  166.    return lQueue;
  167. }
  168.  
  169.