home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12546.ZIP / TRACER.C < prev    next >
Text File  |  1990-03-19  |  15KB  |  462 lines

  1. //----------------------------------------------
  2. //-TRACER ( OS/2 PM Debugging Trace Facility )--
  3. //----------------------------------------------
  4. //-(c) 1990 Daniel Hildebrand-------------------
  5. //----------------------------------------------
  6.  
  7. #define INCL_PM
  8. #define INCL_WIN
  9. #define INCL_DOS
  10. #define INCL_WINLISTBOXES
  11. #define INCL_WINHOOKS
  12. #define INCL_ERRORS
  13.  
  14. #include <os2.h>
  15. #include <process.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "tracer.h"
  19.  
  20. //----------------------------------------------
  21. //-Module declarations--------------------------
  22. //----------------------------------------------
  23. int               main          ( void );
  24. MRESULT EXPENTRY  TracerWndProc ( HWND hWnd, USHORT msg,
  25.                                   MPARAM mp1, MPARAM mp2 );
  26. VOID              TracerCommand ( HWND hWnd, SHORT id,
  27.                                   SHORT source, BOOL mouse );
  28. static VOID       ClearSelector ( char far * selector_string );
  29. BOOL EXPENTRY     HelpHook      ( HAB hab, USHORT usMode,
  30.                                   USHORT idTopic, USHORT idSubTopic,
  31.                                   PRECTL prcPosition );
  32. static VOID       HelpMessage   ( void );
  33.  
  34. //----------------------------------------------
  35. //-External references--------------------------
  36. //----------------------------------------------
  37. extern
  38. void FAR PASCAL   TracerPaint      ( HWND hWnd, USHORT msg,
  39.                                      MPARAM mp1, MPARAM mp2 );
  40. extern
  41. void FAR PASCAL   ClearLB          ( HWND hLB );
  42. extern
  43. VOID              LogToFile        ( PSZ selector_string );
  44. extern
  45. MRESULT EXPENTRY  TracerAboutDlg   ( HWND hWndDlg,
  46.                                      USHORT message,
  47.                                      MPARAM mp1, MPARAM mp2 );
  48. extern
  49. MRESULT EXPENTRY  TracerLogFileDlg ( HWND hWndDlg,
  50.                                      USHORT message,
  51.                                      MPARAM mp1, MPARAM mp2 );
  52.  
  53.  
  54. //----------------------------------------------
  55. //-Global variables-----------------------------
  56. //----------------------------------------------
  57. HAB        hAB;                           /* anchor block   */
  58. HMQ        hmqTracer;                     /* handle queue   */
  59. HSYSSEM    hSysSem;                       /* sys semaphore  */
  60. HFILE      pLogFile;                      /* handle to file */
  61. HWND       hParentWnd;                    /* client hWnd    */
  62. HWND       hParentFrm;                    /* frame hWnd     */
  63. HWND       hLB;                           /* handle ListBox */
  64. BOOL       bUserRequestsScroll = TRUE;    /* Scroll ON ?    */
  65. BOOL       bUserRequestsNewLog = TRUE;    /* Truncate Log?  */
  66. BOOL       bUserRequestsLog    = FALSE;   /* LogFile ON ?   */
  67. BOOL       bLoadFail;                     /* TRACER Load OK?*/
  68. char       szMessage[]     =   "           PM TRACER             ";
  69.                                           /* Log File       */
  70. char       szLogFile[]     =   "\\tracer.fle";
  71.                                           /* Window Class   */
  72. char       szParentClass[] =   "PClass";
  73. char  far  *selector_string; /* pointer to named shared mem */
  74. int        iNumberItems;                  /* # error strings*/
  75. USHORT     selector;         /* selector to named shared mem*/
  76. USHORT     pusAction;                     /*  file IO       */
  77. USHORT     pusBytesWritten;               /*  file IO       */
  78.  
  79. //----------------------------------------------
  80. //-main()---------------------------------------
  81. //----------------------------------------------
  82.  
  83. int main( )
  84. {
  85.     QMSG      qmsg;
  86.     ULONG     ctldata;
  87.     SWP       swpCurrent;
  88.     int       aiGen;
  89.  
  90.     // initialize this process
  91.     hAB = WinInitialize( NULL );
  92.  
  93.     // create a PM message queue
  94.     hmqTracer = WinCreateMsgQueue( hAB, 0 );
  95.  
  96.     // register window class
  97.     if ( !WinRegisterClass( hAB,
  98.                             (PCH)szParentClass,
  99.                             (PFNWP)TracerWndProc,
  100.                             CS_SIZEREDRAW,
  101.                             0 ) )
  102.         return( 0 );
  103.  
  104.     // file control flags
  105.     ctldata = FCF_TITLEBAR      | FCF_SYSMENU  | FCF_BORDER |
  106.               FCF_MINBUTTON     | FCF_MENU     | FCF_ICON   |
  107.               FCF_SHELLPOSITION | FCF_TASKLIST | FCF_ACCELTABLE;
  108.  
  109.     // create standard window
  110.     hParentFrm = WinCreateStdWindow( HWND_DESKTOP,
  111.                                      WS_SYNCPAINT,
  112.                                      &ctldata,
  113.                                      (PCH)szParentClass,
  114.                                      NULL,
  115.                                      0L,
  116.                                      (HMODULE)NULL,
  117.                                      TRACERICON,
  118.                                      (HWND FAR *)&hParentWnd );
  119.  
  120.     // if semaphore was obtained
  121.     // if named shared memory segment was obtained
  122.     // if tracer file was opened
  123.     // ...
  124.     if ( ! bLoadFail )
  125.     {
  126.  
  127.         // auditory feedback to user that app coming up OK.
  128.         for ( aiGen = 0; aiGen < 12; aiGen += 2 )
  129.              DosBeep( ((aiGen + 1) * 100), 1 );
  130.  
  131.         WinSetWindowText( hParentFrm, "OS\\2 TRACER Version 1.0");
  132.         WinSetWindowPos ( hParentFrm, HWND_TOP,
  133.                           20,
  134.                           10,
  135.                           500,
  136.                           125,
  137.                           SWP_SIZE | SWP_MOVE | SWP_SHOW );
  138.     }
  139.  
  140.     // enter message loop
  141.     while( WinGetMsg( hAB, (PQMSG)&qmsg, (HWND)NULL, 0, 0 ) )
  142.         WinDispatchMsg( hAB, (PQMSG)&qmsg );
  143.  
  144.     // destroy TRACER window
  145.     WinDestroyWindow( hParentFrm );
  146.     // destroy message queue
  147.     WinDestroyMsgQueue( hmqTracer );
  148.     // bye bye
  149.     WinTerminate( hAB );
  150. }
  151.  
  152. //----------------------------------------------
  153. //-TracerWndProc()------------------------------
  154. //----------------------------------------------
  155. //----------------------------------------------
  156.  
  157. MRESULT EXPENTRY TracerWndProc( HWND hWnd, USHORT msg,
  158.                                 MPARAM mp1, MPARAM mp2 )
  159. {
  160. RECTL    rRect;
  161. int      aiGen;
  162.  
  163.     switch (msg)
  164.     {
  165.  
  166.     case WM_CREATE:
  167.  
  168.         // if semaphore already exists ...
  169.         if ( DosCreateSem( CSEM_PUBLIC, &hSysSem,
  170.                                           TRACER_SEMAPHORE ) )
  171.             // ... and we cannot open it, then abort
  172.             if ( DosOpenSem( &hSysSem, TRACER_SEMAPHORE ) )
  173.             {
  174.                 DosBeep(300,300);
  175.                 WinMessageBox( HWND_DESKTOP, hWnd,
  176.                                (PCH)"Failed to Load!",
  177.                                (PCH)"OS/2 TRACER Version 1.0", NULL,
  178.                                MB_OK| MB_ICONEXCLAMATION );
  179.                 bLoadFail = 1;
  180.                 WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  181.                 break;
  182.             }
  183.  
  184.         // allocate an 81 byte data segment
  185.         if ( DosAllocShrSeg( 81, TRACER_SEGMENT,
  186.                                   &selector ) )
  187.         {
  188.             DosBeep(300,300);
  189.             WinMessageBox( HWND_DESKTOP, hWnd,
  190.                            (PCH)"Failed to Load!",
  191.                            (PCH)"OS/2 TRACER Version 1.0", NULL,
  192.                            MB_OK| MB_ICONEXCLAMATION );
  193.             bLoadFail = 1;
  194.             WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  195.             break;
  196.         }
  197.  
  198.         // open TRACER file
  199.         DosOpen( (PSZ)szLogFile, &pLogFile, &pusAction, 100L,
  200.                                             0,
  201.                                             0x11,
  202.                                             0x41, 0L );
  203.  
  204.         // obtain a pointer to the named segment
  205.         selector_string =
  206.                  (char far *)((unsigned long)selector << 16);
  207.  
  208.         // clear the segment
  209.         ClearSelector( selector_string );
  210.  
  211.         // F1 help hook
  212.         WinSetHook( hAB, hmqTracer, HK_HELP, (PFN)HelpHook, NULL );
  213.  
  214.         // Send to anyone who was brought up BEFORE the TRACER
  215.         WinBroadcastMsg( hWnd, TRACER_RECIEVE_HANDLE,
  216.                               MPFROMHWND( hWnd ), 0L,
  217.                               BMSG_FRAMEONLY | BMSG_POSTQUEUE );
  218.  
  219.         return( WinDefWindowProc( hWnd, msg, mp1, mp2 ) );
  220.  
  221.         break;
  222.  
  223.     case WM_COMMAND:
  224.  
  225.             // handle menu commands
  226.             TracerCommand( hWnd, SHORT1FROMMP(mp1),
  227.                       SHORT1FROMMP(mp2), SHORT2FROMMP(mp2));
  228.             break;
  229.  
  230.     case TRACER_REQUEST_HANDLE:
  231.  
  232.          // a client has broadcasted a request for handshake
  233.          WinPostMsg( HWNDFROMMP(mp1), TRACER_RECIEVE_HANDLE,
  234.                            MPFROMHWND(hWnd), 0L );
  235.  
  236.         break;
  237.  
  238.     case TRACER_REQUEST_ACTION:
  239.  
  240.          // client has issued a TRACER() request
  241.  
  242.          // is screen toggled ON ?
  243.          if ( bUserRequestsScroll )
  244.          {
  245.              selector_string[80] = '\0';
  246.  
  247.              WinSendMsg( hLB, LM_INSERTITEM,
  248.                 (MPARAM)-1L, (MPARAM)(PCH)selector_string );
  249.              WinSendMsg( hLB, LM_SELECTITEM,
  250.                 (MPARAM)(iNumberItems - 1), (MPARAM)FALSE );
  251.              WinSendMsg( hLB, LM_SELECTITEM,
  252.                          (MPARAM)iNumberItems++, (MPARAM)TRUE );
  253.              WinSendMsg( hLB, LM_SETTOPINDEX,
  254.                          (MPARAM)(iNumberItems - 1),
  255.                          (MPARAM)TRUE );
  256.          }
  257.  
  258.          // is file toggled ON ?
  259.          if ( bUserRequestsLog )
  260.          {
  261.              selector_string[80] = '\0';
  262.              LogToFile( selector_string );
  263.  
  264.          }
  265.  
  266.          ClearSelector( selector_string );
  267.  
  268.         break;
  269.  
  270.     case WM_CLOSE:
  271.  
  272.         // return semaphore to system
  273.         DosCloseSem( hSysSem );
  274.  
  275.         // is semaphore available to be obtained ?
  276.         if ( DosCreateSem( CSEM_PUBLIC, &hSysSem,
  277.                                TRACER_SEMAPHORE ) )
  278.         {
  279.             // that's no good.  a client must still have it opened !
  280.             DosBeep(75,200);
  281.             WinMessageBox( HWND_DESKTOP, hWnd,
  282.             (PCH)
  283.             "Will not shut down while client(s) are still attached!",
  284.             (PCH)"OS/2 TRACER Version 1.0", NULL,
  285.                              MB_OK| MB_ICONEXCLAMATION );
  286.             DosOpenSem( &hSysSem, TRACER_SEMAPHORE );
  287.             break;
  288.         }
  289.         else
  290.             DosCloseSem( hSysSem );  /* one for create */
  291.  
  292.         // continue close down
  293.         WinPostMsg( hWnd, TRACER_MYCLOSE, 0L, 0L );
  294.  
  295.         break;
  296.  
  297.     case TRACER_MYCLOSE:
  298.  
  299.         // free the named shared data segment
  300.         if ( DosFreeSeg( selector ) ) DosBeep( 100, 450 );
  301.  
  302.         // release the F1 help hook
  303.         WinReleaseHook ( hAB, hmqTracer, HK_HELP,
  304.                                  (PFN)HelpHook, NULL );
  305.  
  306.         // flush the buffer to the TRACER file / close it
  307.         DosBufReset( pLogFile );
  308.         DosClose( pLogFile );
  309.  
  310.         // auditory feedback to user that app coming down OK.
  311.         for ( aiGen = 12; aiGen > 0; aiGen -= 2 )
  312.              DosBeep( ((aiGen - 1) * 100), 1 );
  313.  
  314.         WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  315.  
  316.         break;
  317.  
  318.     case WM_PAINT:
  319.  
  320.         // paint routine for TRACER window
  321.         TracerPaint( hWnd, msg, mp1, mp2 );
  322.  
  323.         break;
  324.  
  325.     case WM_ERASEBACKGROUND:
  326.  
  327.         return( TRUE );
  328.  
  329.         break;
  330.  
  331.     default:
  332.  
  333.         return( WinDefWindowProc( hWnd, msg, mp1, mp2 ) );
  334.  
  335.         break;
  336.     }
  337.  
  338.     return(0L);
  339. }
  340.  
  341. //----------------------------------------------
  342. //--HelpHook------------------------------------
  343. //----------------------------------------------
  344. //----------------------------------------------
  345.  
  346. BOOL EXPENTRY HelpHook( HAB hab, USHORT usMode,
  347.      USHORT idTopic, USHORT idSubTopic, PRECTL prcPosition )
  348. {
  349.  
  350. WinMessageBox( HWND_DESKTOP, hParentWnd,
  351. (PCH)
  352. "TRACER.H has instructions on how to use TRACER in your app.",
  353.                    (PCH)"OS/2 TRACER HelpHook", NULL,
  354.                                   MB_OK| MB_CUANOTIFICATION );
  355.  
  356.     #ifdef HelpHook
  357.     switch ( usMode )
  358.     {
  359.  
  360.       case HLPM_MENU:
  361.  
  362.       // idtopic is submenu identifier
  363.       // idsubtopic is item identifier
  364.       // prcposition is boundary of item
  365.  
  366.            switch ( idTopic )
  367.            {
  368.              case ID_HELPBUTTON:
  369.              HelpMessage();
  370.            }
  371.       break;
  372.       case HLPM_FRAME:
  373.  
  374.       // idtopic is frame identifier
  375.       // idsubtopic is focus window identifier
  376.       // prcposition is boundary of focus window
  377.  
  378.            switch ( idTopic )
  379.            {
  380.              case ID_HELPBUTTON:
  381.              HelpMessage();
  382.            }
  383.       break;
  384.  
  385.       case HLPM_WINDOW:
  386.  
  387.       // idtopic is parent of focus window
  388.       // idsuptopic is focus window identifier
  389.       // prcposition is boundary of focus window
  390.  
  391.            switch ( idTopic )
  392.            {
  393.              case ID_HELPBUTTON:
  394.              HelpMessage();
  395.            }
  396.       break;
  397.  
  398.     }
  399.     #endif
  400. return TRUE;
  401. }
  402.  
  403. //----------------------------------------------
  404. //--TracerCommand-------------------------------
  405. //----------------------------------------------
  406. //----------------------------------------------
  407.  
  408. VOID TracerCommand( HWND hWnd, SHORT id, SHORT source, BOOL mouse )
  409. {
  410.  
  411.     switch( id )
  412.     {
  413.         case IDMABOUT:
  414.  
  415.             WinDlgBox( HWND_DESKTOP, hWnd, (PFNWP)TracerAboutDlg,
  416.                             NULL, IDMABOUT, NULL );
  417.             break;
  418.  
  419.         case IDMLOG:
  420.  
  421.             WinDlgBox( HWND_DESKTOP, hWnd, (PFNWP)TracerLogFileDlg,
  422.                             NULL, IDMLOG, NULL );
  423.             break;
  424.  
  425.         case IDMCLEAR:
  426.  
  427.             ClearLB( hLB );
  428.             break;
  429.  
  430.     }
  431. }
  432.  
  433. //----------------------------------------------
  434. //-HelpMessage()--------------------------------
  435. //----------------------------------------------
  436. //----------------------------------------------
  437.  
  438. static VOID       HelpMessage   ( void )
  439. {
  440.  
  441.  WinMessageBox( HWND_DESKTOP, hParentWnd,
  442.                 (PCH)
  443.  "TRACER.H has instructions on how to use TRACER in your app.",
  444.                 (PCH)"OS/2 TRACER HelpHook", NULL,
  445.                                    MB_OK| MB_CUANOTIFICATION );
  446. return;
  447. }
  448.  
  449. //----------------------------------------------
  450. //-ClearSelector()------------------------------
  451. //----------------------------------------------
  452. //----------------------------------------------
  453.  
  454. static VOID ClearSelector  ( PSZ selector_string )
  455. {
  456. int aiX;
  457.  
  458.     for ( aiX = 0; aiX < 80; aiX++ )
  459.          selector_string[aiX] = ' ';
  460. return;
  461. }
  462.