home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CFEXEC.ZIP / CFEXEC.C < prev    next >
C/C++ Source or Header  |  1990-03-20  |  8KB  |  284 lines

  1. #define INCL_BASE
  2. #define INCL_NOPM
  3. #include <os2.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. typedef struct _EXECBUNDLE
  9.    {
  10.    CHAR        szFailName [128];
  11.    USHORT      fExec;
  12.    CHAR        szArgs [128];
  13.    CHAR        szEnvironment [255];
  14.    RESULTCODES resc;
  15.    CHAR        szPgmName [128];
  16.    } EXECBUNDLE;
  17. typedef EXECBUNDLE FAR *PEXECBUNDLE;
  18.  
  19.  
  20. EXECBUNDLE      aebnd [20];
  21. PID             pid;
  22. SHORT           sMaxExec;
  23.  
  24. VOID            CfaStartWatchRange (SHORT sStart, SHORT sStop, LONG lDelay);
  25. VOID            CfaStartWatchAll (SHORT sStart, SHORT sStop);
  26. USHORT          CfaGetMakeInput (VOID);
  27. USHORT          CfaGetExecInput (VOID);
  28. CHAR            *CfaStrTrim (CHAR *pszLine);
  29. VOID            main (int argc, char *argv []);
  30.  
  31. /****************************************************************************/
  32. VOID            main (int argc, char *argv [])
  33. {
  34. PIDINFO         pidi;
  35.  
  36. if (argc != 3)
  37.    {
  38.    printf ("\n USAGE : CFEXEC <mode 1 or 2><Exec delay in Milliseconds>\n");
  39.    return;
  40.    }
  41.  
  42. sMaxExec = CfaGetExecInput ();
  43. if (!sMaxExec)
  44.    {
  45.    return;
  46.    }
  47. DosGetPID (&pidi);
  48.  
  49. if (argv [1][0] == '1')
  50.    {
  51.    DosSetPrty (PRTYS_PROCESSTREE, PRTYC_IDLETIME, 0, pidi.pid);
  52.    CfaStartWatchRange (0, sMaxExec - 1, atol (argv [2]));
  53.    }
  54. else
  55.    {
  56.    DosSetPrty (PRTYS_PROCESSTREE, PRTYC_REGULAR, 0, pidi.pid);
  57.    CfaStartWatchAll (0, sMaxExec - 1);
  58.    }
  59. DosSetPrty (PRTYS_PROCESSTREE, PRTYC_REGULAR, 0, pidi.pid);
  60. }
  61. /****************************************************************************/
  62. VOID            CfaStartWatchRange (SHORT sStart, SHORT sStop, LONG lDelay)
  63. {
  64. SHORT           iI;
  65. USHORT          usRC;
  66.  
  67.  
  68. for (iI = sStart; iI <= sStop; iI++)
  69.    {
  70.    if ((usRC = DosExecPgm (aebnd [iI].szFailName,
  71.                sizeof (aebnd [iI].szFailName),
  72.                aebnd [iI].fExec,
  73.                aebnd [iI].szArgs,
  74.                0,
  75.                &(aebnd [iI].resc),
  76.                aebnd [iI].szPgmName)) != NULL)
  77.       {
  78.       DosBeep (100, 400);
  79.       fprintf (stderr, "Exited on exec %d with ecode %d", iI, usRC);
  80.       DosExit (EXIT_PROCESS, -1);
  81.       }
  82.    else
  83.       {
  84.       DosSleep (lDelay);
  85.       }
  86.    }
  87.  
  88. for (iI = sStart; iI <= sStop; iI++)
  89.    {
  90.    if ((usRC = DosCwait (DCWA_PROCESS,
  91.           DCWW_WAIT,
  92.           &(aebnd [iI].resc),
  93.           &pid,
  94.           aebnd [iI].resc.codeTerminate)) != NULL)
  95.       {
  96.       DosBeep (100, 400);
  97.       fprintf (stderr, "Exited on cwait %d with ecode %d", iI, usRC);
  98.       DosExit (EXIT_PROCESS, -1);
  99.       }
  100.    else
  101.       {
  102.       fprintf (stderr, "Process %d done : Term %d Res %d\n", iI,
  103.                            aebnd [iI].resc.codeTerminate,
  104.                            aebnd [iI].resc.codeResult);
  105.       }
  106.    }
  107. }
  108. /****************************************************************************/
  109. VOID            CfaStartWatchAll (SHORT sStart, SHORT sStop)
  110. {
  111. SHORT           iI;
  112. USHORT          usRC;
  113.  
  114. for (iI = sStart; iI <= sStop; iI++)
  115.    {
  116.    if ((usRC = DosExecPgm (aebnd [iI].szFailName,
  117.                sizeof (aebnd [iI].szFailName),
  118.                aebnd [iI].fExec,
  119.                aebnd [iI].szArgs,
  120.                0,
  121.                &(aebnd [iI].resc),
  122.                aebnd [iI].szPgmName)) != NULL)
  123.       {
  124.       DosBeep (100, 400);
  125.       fprintf (stderr, "Exited on exec %d with ecode %d", iI, usRC);
  126.       DosExit (EXIT_PROCESS, -1);
  127.       }
  128.    else
  129.       {
  130.       if ((iI < sStop) && (stricmp (aebnd [iI].szPgmName, aebnd [iI + 1].szPgmName)))
  131.      {
  132.      if ((usRC = DosCwait (DCWA_PROCESS,
  133.                    DCWW_WAIT,
  134.                    &(aebnd [iI].resc),
  135.                    &pid,
  136.                    aebnd [iI].resc.codeTerminate)) != NULL)
  137.         {
  138.         DosBeep (100, 400);
  139.         fprintf (stderr, "Exited on cwait %d with ecode %d", iI, usRC);
  140.         DosExit (EXIT_PROCESS, -1);
  141.         }
  142.      else
  143.         {
  144.         fprintf (stderr, "Process %d done : Term %d Res %d\n", iI,
  145.                        aebnd [iI].resc.codeTerminate,
  146.                        aebnd [iI].resc.codeResult);
  147.         }
  148.  
  149.      }
  150.       else if (iI == sStart)
  151.          {
  152.          ; 
  153.          }
  154.       else if (!stricmp (aebnd [iI].szPgmName, aebnd [iI - 1].szPgmName))
  155.          {
  156.          if ((usRC = DosCwait (DCWA_PROCESS,
  157.                            DCWW_WAIT,
  158.                    &(aebnd [iI - 1].resc),
  159.                                &pid,
  160.                    aebnd [iI - 1].resc.codeTerminate)) != NULL)
  161.             {
  162.             DosBeep (100, 400);
  163.         fprintf (stderr, "Exited on cwait %d with ecode %d", iI - 1, usRC);
  164.             DosExit (EXIT_PROCESS, -1);
  165.             }
  166.          else
  167.             {
  168.         fprintf (stderr, "Process %d done : Term %d Res %d\n", iI - 1,
  169.                        aebnd [iI - 1].resc.codeTerminate,
  170.                        aebnd [iI - 1].resc.codeResult);
  171.         }
  172.  
  173.          }
  174.       }
  175.    }
  176.  
  177. /* Wait for the last child in the range to finish */
  178. if ((usRC = DosCwait (DCWA_PROCESS,
  179.               DCWW_WAIT,
  180.               &(aebnd [sStop].resc),
  181.               &pid,
  182.               aebnd [sStop].resc.codeTerminate)) != NULL)
  183.    {
  184.    DosBeep (100, 400);
  185.    fprintf (stderr, "Exited on cwait %d with ecode %d", sStop, usRC);
  186.    DosExit (EXIT_PROCESS, -1);
  187.    }
  188. else
  189.    {
  190.    fprintf (stderr, "Process %d done : Term %d Res %d\n", sStop,
  191.                        aebnd [sStop].resc.codeTerminate,
  192.                        aebnd [sStop].resc.codeResult);
  193.    }
  194. }
  195. /****************************************************************************/
  196. USHORT          CfaGetExecInput (VOID)
  197. {
  198. SHORT           iI;
  199. CHAR            szDummy [128];
  200. CHAR            *pszFirstWord;
  201.  
  202. iI = 0;
  203. while (fgets (aebnd [iI].szArgs, sizeof (aebnd [iI].szArgs), stdin))
  204.    {
  205.    aebnd [iI].fExec = EXEC_ASYNCRESULT;
  206.    CfaStrTrim (aebnd [iI].szArgs);
  207.    aebnd [iI].szArgs [strlen (aebnd [iI].szArgs) - 1] = '\0';
  208.    aebnd [iI].szArgs [strlen (aebnd [iI].szArgs) + 1] = '\0';
  209.    strcpy (szDummy, aebnd [iI].szArgs);
  210.    pszFirstWord = strtok (szDummy, " \t\n");
  211.  
  212.    fprintf (stderr, "%s\n", aebnd [iI].szArgs);
  213.    if (!stricmp (pszFirstWord, "cl"))
  214.       {
  215.       aebnd [iI].szArgs [2] = '\0';
  216.       strcpy (aebnd [iI].szPgmName, "cl.exe");
  217.       iI++;
  218.       }
  219.    /*
  220.    else if (!stricmp (pszFirstWord, "rc"))
  221.       {
  222.       aebnd [iI].szArgs [2] = '\0';
  223.       strcpy (aebnd [iI].szPgmName, "rc.exe");
  224.       iI++;
  225.       }
  226.    else if (!stricmp (pszFirstWord, "link"))
  227.       {
  228.       aebnd [iI].szArgs [4] = '\0';
  229.       strcpy (aebnd [iI].szPgmName, "link.exe");
  230.       iI++;
  231.       }
  232.    else if (!stricmp (pszFirstWord, "mapsym"))
  233.       {
  234.       aebnd [iI].szArgs [6] = '\0';
  235.       strcpy (aebnd [iI].szPgmName, "mapsym.exe");
  236.       iI++;
  237.       }
  238.    else if (!stricmp (pszFirstWord, "implib"))
  239.       {
  240.       aebnd [iI].szArgs [6] = '\0';
  241.       strcpy (aebnd [iI].szPgmName, "implib.exe");
  242.       iI++;
  243.       }
  244.    else if (!stricmp (pszFirstWord, "bind"))
  245.       {
  246.       aebnd [iI].szArgs [4] = '\0';
  247.       strcpy (aebnd [iI].szPgmName, "bind.exe");
  248.       iI++;
  249.       }
  250.    else if (!stricmp (pszFirstWord, "echo"))
  251.       {
  252.       aebnd [iI].szArgs [4] = '\0';
  253.       strcpy (aebnd [iI].szPgmName, "echo");
  254.       iI++;
  255.       }
  256.    */
  257.    }
  258. return (iI);
  259. }
  260. /****************************************************************************/
  261. CHAR            *CfaStrTrim (CHAR *pszLine)
  262. {
  263. SHORT           sCount;
  264. size_t          sizetStrLength;
  265.  
  266. sizetStrLength = strlen (pszLine);
  267. sCount = 0;
  268. while (pszLine [sCount] == ' ')
  269.    {
  270.    sCount += 1;
  271.    }
  272. memmove (pszLine, pszLine + sCount, sizetStrLength - sCount);
  273. pszLine [sizetStrLength - sCount] = '\0';
  274. sizetStrLength = strlen (pszLine);
  275. sCount = sizetStrLength - 1;
  276. while (pszLine [sCount] == ' ')
  277.    {
  278.    sCount -= 1;
  279.    }
  280. pszLine [sCount + 1] = '\0';
  281. return (pszLine);
  282. }
  283. /****************************************************************************/
  284.