home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / TOOLS / QUITPROG / quitprog.c < prev    next >
Text File  |  1997-09-06  |  7KB  |  194 lines

  1. /******************************** Module Head ***************************
  2. * Author      : J¢rgen Thomsen (j.thomsen@usa.net)                      *
  3. *               (Please send me an email, if you use this program)      *
  4. * File/Modul  : QuitProg.C                                              *
  5. * Program type: OS/2 command line program                               *
  6. * Short descr.: Makes OS/2 PM program save data and/or close and exit   *
  7. * Keywords    : OS/2, CLOSE, EXIT, TERMINATE, SAVE, WM_QUIT, PROGRAM    *
  8. * Description : Will send a WM_SAVEAPLLICATION and/or a WM_QUIT message *
  9. *               to all tasks in the task list containing a certain      *
  10. *               string in the title.                                    *
  11. * Language    : IBM C Set++ version 2.0                                 *
  12. * INPUT       : command line parameters                                 * 
  13. *               <search string> string to be found in title, not case   *
  14. *                               sensitive EPM or "big program"          *
  15. *               <msgtype>       S, Q, SQ for save data, quit program or *
  16. *                               both. Default: Q                        *
  17. *               <timeout>       seconds, wait for specified time for    *
  18. *                               programs to terminate. Default: 60      *
  19. *               e.g. QuitProg EPM Q 10                                  *
  20. * OUTPUT:       message for each command sent to each program on stdout *
  21. *               non-zero return code in case of error or timeout        *
  22. * ------------+----------------+--------------------------------------- *
  23. * Revision    | Date     / ID  | Changes                                *
  24. * ------------+----------------+--------------------------------------- *
  25. *   1.00      | 05-09-97 / J¥T | Initial release                        *
  26. *************************************************************************/
  27. #define INCL_NOCOMMON
  28. #define INCL_DOSPROCESS   /* Process and thread values */
  29.  
  30. #define INCL_WINWINDOWMGR
  31. #define INCL_WINMESSAGEMGR
  32. #define INCL_WINERRORS
  33. #define INCL_WINSWITCHLIST
  34.  
  35. #include <os2.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38.  
  39. #ifdef XPQ /* informational only */
  40.       typedef struct _SWCNTRL          /* swctl */
  41.       {
  42.          HWND     hwnd;
  43.          HWND     hwndIcon;
  44.          HPROGRAM hprog;
  45.          PID      idProcess;
  46.          ULONG    idSession;
  47.          ULONG    uchVisibility;
  48.          ULONG    fbJump;
  49.          CHAR     szSwtitle[MAXNAMEL+4];
  50.          ULONG    bProgType;
  51.       } SWCNTRL;
  52.       
  53.       typedef SWCNTRL *PSWCNTRL;
  54.             
  55.       typedef struct _SWENTRY          /* swent */
  56.       {
  57.          HSWITCH hswitch;
  58.          SWCNTRL swctl;
  59.       } SWENTRY;
  60.       typedef SWENTRY *PSWENTRY;
  61.       
  62.       typedef struct _SWBLOCK          /* swblk */
  63.       {
  64.          ULONG    cswentry;
  65.          SWENTRY aswentry[1];
  66.       } SWBLOCK;
  67.  
  68.       typedef SWBLOCK *PSWBLOCK;
  69.  
  70. #endif
  71.  
  72. #define MAXBUF 32000
  73. #define DELAYVAL 500  /* ms */
  74.  
  75. CHAR    buf[MAXBUF];
  76. APIRET  rc = 0;
  77. USHORT  res = 0;
  78. CHAR    buf1[MAXNAMEL+4];
  79.  
  80. void main (int argc, char *argv[])
  81. {
  82.  int      i = 0, cnt = 0;
  83.  USHORT   res = 2;
  84.  CHAR     *pstr = NULL;
  85.  
  86.  HAB      hab = NULLHANDLE;                  /* PM anchor block handle         */
  87.  HMQ      hmq = NULLHANDLE;                  /* message queue handle           */
  88.  
  89.  ULONG    cbItems = 0, il;
  90.  LONG     timout = 60;
  91.  PVOID    pBase = NULL;
  92.  PSWBLOCK pswblk = NULL, pswblk1 = NULL;
  93.  SWCNTRL  *pswcntrl = NULL;
  94.  ERRORID  err = 0;
  95.  PVOID    SaveAction = NULL, QuitAction = NULL;
  96.  
  97.  if (argc < 2 || argc > 4) /* 1 parameter required */
  98.  { 
  99.     printf("Syntax: %s <Search string> [ [SQ|S|Q] [<timeout sec. (def. 60)>]]\n", argv[0]);
  100.     exit(1);
  101.    }
  102.    pstr = argv[1];
  103.    strlwr(argv[1]);
  104.    if (argc == 4) { timout = atoi(argv[3]);} 
  105.    timout = timout * 1000 / DELAYVAL;
  106.    if (argc >= 3) { 
  107.       strupr(argv[2]); 
  108.       SaveAction = strchr(argv[2], 'S');
  109.       QuitAction = strchr(argv[2], 'Q'); } /* endif */
  110.    if (QuitAction == NULL) {timout = 0;} /* endif */
  111.  
  112.    hab = WinInitialize( 0UL );
  113.    hmq = WinCreateMsgQueue( hab, 0UL ); /* not used and created only if compiled as PM program */
  114.  
  115. /*
  116.    This code calls WinQuerySwitchList to fill the buffer with the information about
  117.    each program in the Task List.
  118. */
  119.  
  120.       pswblk = (PSWBLOCK) &buf[0];
  121.       cbItems = WinQuerySwitchList(hab, pswblk, MAXBUF);       /* gets struct. array */
  122.  
  123. /* check each task */
  124.       for (i=0; i < cbItems ; i++ ) {
  125.          pswcntrl = &pswblk->aswentry[i].swctl;
  126.          pstr = &pswblk->aswentry[i].swctl.szSwtitle[0];
  127.          strcpy(buf1, pstr);  
  128.          strlwr(buf1);
  129.          if (strstr(buf1, argv[1])) { 
  130.            res = 0; cnt++;
  131.  
  132.          if (SaveAction) {
  133.             /* send WM_SAVEAPPLICATION message */
  134.              printf("Sending save command to '%s'\n", pstr);
  135.              WinPostMsg(pswcntrl->hwnd, WM_SAVEAPPLICATION,
  136.                         MPFROM2SHORT(0, 0), MPFROM2SHORT(0, 0));
  137.  
  138.              err = WinGetLastError(hab) & 0xffff;
  139.              if (err && err != PMERR_NOT_IN_A_PM_SESSION) {
  140.                printf("Error on save: %x\n", err);
  141.              }
  142.          } /* endif */
  143.  
  144.          if (QuitAction) {
  145.          /* send WM_QUIT message */
  146.             printf("Sending quit command to '%s'\n", pstr);
  147.             WinPostMsg(pswcntrl->hwnd, WM_QUIT,
  148.                        MPFROM2SHORT(0, 0), MPFROM2SHORT(0, 0));
  149.             err = WinGetLastError(hab) & 0xffff;
  150.             if (err && err != PMERR_NOT_IN_A_PM_SESSION) {
  151.               if (err == PMERR_INVALID_HWND ) { printf(" Invalid window handle\n");
  152.               } else { 
  153.                 if (err == PMERR_WINDOW_NOT_LOCKED ){ printf(" Window not locked\n");
  154.                 } else {
  155.                    printf("Error on quit: %x\n", err);
  156.                 } /* endif */
  157.               }
  158.             }
  159.           } /* end if QuitAction */
  160.          } /* end if strstr */
  161.       } /* endfor */
  162.  
  163.       if (timout != 0) { /* quit specified, wait for progs to terminate */
  164.         do {     
  165.           cbItems = WinQuerySwitchList(hab, pswblk, MAXBUF);       /* gets struct. array */
  166.           cnt = 0;
  167.           for (i=0; i < cbItems ; i++ ) {
  168.             pstr = &pswblk->aswentry[i].swctl.szSwtitle[0]; 
  169.             strcpy(buf1, pstr); strlwr(buf1);
  170.             if (strstr(buf1, argv[1])) { 
  171.               cnt++;
  172.               if (timout == 1) {printf("Timeout on '%s'. Not terminated!\n", pstr); res = 3;}
  173.             } /* endif */
  174.           } /* endfor */
  175.  
  176.           if (cnt > 0) {rc = DosSleep(DELAYVAL); timout--;} /* endif */
  177.         } while (cnt > 0 && timout > 0 ); /* enddo */
  178.  
  179.       } /* end if timout */
  180.  
  181.     /* Destroy the possible message queue and release the anchor block. */
  182.  
  183.    if ( hmq != NULLHANDLE )
  184.       WinDestroyMsgQueue( hmq );
  185.  
  186.    if ( hab != NULLHANDLE )
  187.       WinTerminate( hab );
  188.  
  189. exit(res);
  190. } /* end main */
  191.  
  192.  
  193.  
  194.