home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RCPOPUP.ZIP / PLTEST.C < prev    next >
C/C++ Source or Header  |  1992-10-14  |  6KB  |  135 lines

  1. /**************************************************************************
  2. *
  3. *   Function: pltest
  4. *
  5. *    Purpose: Test popuplst.  Generally this program will execute (DosExecPgm)
  6. *             a specified program and send the program a handle to a response
  7. *             pipe and whatever arguments were given on the command line
  8. *
  9. *   To build: pmake pltest.mak
  10. *
  11. *     To use: pltest testfunc args
  12. *                    ^--------^---- Function to be tested
  13. *                             ^-----Arguments to send to the function
  14. *   Defaults: none
  15. *
  16. *       File: popuplst.c
  17. *
  18. *   Abstract: Pltest simply opens a pipe for response, invokes the specified
  19. *             function with the following arguments: 1) the ascii decimal
  20. *             handle to the reponse pipe and 2) whatever arguments were passed
  21. *             to pltest.  On completion, the ascii response string from the
  22. *             function being tested is printed.
  23. *
  24. **************************************************************************/
  25.  
  26. #define INCL_DOSFILEMGR
  27. #define INCL_DOSPROCESS
  28. #define INCL_DOSQUEUES
  29. #define INCL_DOSSEMAPHORES
  30. #define INCL_DOSSESMGR
  31. #include <os2.h>
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36.  
  37. /* Local Routines */ 
  38. #define ERROR_EXIT(string, rcvd_status) \
  39.   { printf("%s with status %d, exiting\n", string, rcvd_status); \
  40.     DosExit(EXIT_PROCESS, rcvd_status); }
  41.  
  42. /*  Global variables */
  43.  
  44. /**************************************************************************
  45. *
  46. *   Function: main
  47. *
  48. *    Purpose: Create pipe and exec program.  Print results.
  49. *
  50. **************************************************************************/
  51.  
  52. int main( int argc, char *argv[] )
  53. {
  54.   CHAR achFailName[128];
  55.   RESULTCODES rescResults;
  56.   HFILE hfRead, hfWrite;
  57.   HEV ReadSyncSem;
  58.   ULONG cbIn, api_err, api_err2;
  59.   char argp[40], pInMsg[40], *tempp;    // Pointer to arguments
  60.   char *argp2;                          /* Pointer to start of actual arguments */
  61.   int ii;
  62.   STARTDATA stdat;
  63.   ULONG sess_id, pid;
  64.  
  65.  
  66.         /* Setup Pipe to receive responses on                               */
  67.   api_err = DosCreatePipe(&hfRead, &hfWrite, 4096);
  68.   if (api_err != 0) ERROR_EXIT("DosCreatePipe failed", api_err);
  69.  
  70.         /* Setup arguments to specified function                            */
  71.   tempp = argp;
  72.   tempp += sprintf(argp, "%s", argv[1])+1;  /* Copy pgm name + NULL to args   */
  73.   argp2 = tempp;                        /* Remember where args actually start */
  74.   tempp += sprintf(tempp, "%lu ", hfWrite);  /* Pipe handle to respond to     */
  75.   for (ii=2; ii<argc; ii++)             /* Copy remainder of users arguments  */
  76.     tempp += sprintf(tempp, "\"%s\" ", argv[ii]); /* Next argument            */
  77.   *++tempp = '\0';                      /* 2nd NULL to terminate arglist      */
  78.  
  79.         /* Execute function                                                 */
  80.   stdat.Length      = 30;               /* Only use up to InheritOpt        */
  81.   stdat.Related     = 1;                /* For setsession and restore focus */
  82.   stdat.FgBg        = TRUE;             /* Start session in background OK   */
  83.   stdat.TraceOpt    = 0;                /* No debug tracing                 */
  84.   stdat.PgmTitle    = "Desktop interface";
  85.   stdat.PgmName     = argv[1];          /* Program user is starting         */
  86.   stdat.PgmInputs   = argp2;            /* User's args and response pipe    */
  87.   stdat.TermQ       = NULL;             /* No termination status Queue      */
  88.   stdat.Environment = 0;                /* Inherit current environment      */
  89.   stdat.InheritOpt  = 1;                /* Inherit open handles (resp pipe) */
  90. #if 0
  91.     /* By setting length to 30, the rest of the options get defaults        */
  92.   stdat.SessionType = 0;                /* Let OS/2 choose session style    */
  93.   stdat.IconFile    = NULL;             /* No ICON which we determine       */
  94.   stdat.PgmHandle   = 0L;
  95.   stdat.PgmControl  = 0;                /* Just make it visible             */
  96.   stdat.InitXPos    = 0;                /* Let OS/2 or the program decide   */
  97.   stdat.InitYPos    = 0;
  98.   stdat.InitXSize   = 0;
  99.   stdat.InitYSize   = 0;
  100. #endif
  101.  
  102.   api_err = DosStartSession(&stdat, &sess_id, &pid);
  103.   if (api_err != 0) {
  104.     ERROR_EXIT("DosStartSession failed", api_err);
  105.   }
  106.  
  107.   api_err = DosSleep(1000L);            /* Child must be in tasklist, wait  */
  108.   if (api_err != 0) {
  109.     ERROR_EXIT("DosStartSession failed", api_err);
  110.   }
  111.  
  112.   api_err = DosSelectSession(sess_id);  /* Put our child into foreground    */
  113.   if (api_err != 0) {
  114.     ERROR_EXIT("DosSelectSession failed", api_err);
  115.   }
  116.  
  117.   DosClose(hfWrite);                    /* We only receive from pipe        */
  118.   api_err = DosReadAsync(hfRead, &ReadSyncSem, &api_err2, pInMsg,
  119.                                                       sizeof(pInMsg), &cbIn);
  120.   if (api_err != 0) ERROR_EXIT("DosReadAsync dispatch failed", api_err);
  121.   api_err = DosWaitEventSem(ReadSyncSem, 2*60*1000L);
  122.   if (api_err != 0) ERROR_EXIT("DosWaitEventSem failed", api_err);
  123.   if (api_err2 != 0) ERROR_EXIT("DosReadAsync execution failed", api_err2);
  124.   DosCloseEventSem(ReadSyncSem);
  125.  
  126.   DosClose(hfRead);                     /* Done with read side of pipe now  */
  127.   printf("Normal completion.");
  128.   if (pInMsg[cbIn-1] == '\0') cbIn--;   /* Test for normal string           */
  129.   else printf("  No null terminator in response message.");
  130.   printf("  Response message:\n");      /* Output response message          */
  131.   if (cbIn != 0) fwrite(pInMsg, 1, cbIn, stdout);
  132.   else           fwrite("<NULL>", 1, 6, stdout);
  133.   putchar('\n');
  134. }
  135.