home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmdbterm.zip / DebugTerminal.c next >
C/C++ Source or Header  |  2000-09-23  |  5KB  |  211 lines

  1. #pragma strings(readonly)
  2.  
  3. #define INCL_WINWINDOWMGR
  4. #define INCL_WINFRAMEMGR
  5. #define INCL_WINSHELLDATA
  6. #define INCL_DOSFILEMGR
  7. #define INCL_DOSPROCESS
  8. #define INCL_DOSERRORS
  9.  
  10. #include <os2.h>
  11.  
  12. #include "TerminalWindow.h"
  13. #include "res.h"
  14.  
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #define privateStore (*_threadstore())
  20.  
  21.  
  22. static HWND _Optlink createFrameWindow(HAB hAB);
  23. static HWND _Optlink createClientWindow(HWND hwndFrame, int argc, char *argv[]);
  24.  
  25.  
  26. VOID APIENTRY cleanup(ULONG ulVal);
  27.  
  28.  
  29. typedef struct _THREADDATA
  30. {
  31.    HFILE hfLogFile;
  32.    BOOL fLogFile;
  33. }THREADDATA, *PTHREADDATA;
  34.  
  35. /*
  36.  * argv[1] - pipe handle (required)
  37.  * argv[2] - log filename (optional, default: debug terminal.log)
  38.  */
  39. int main(int argc, char *argv[])
  40. {
  41.    int iRet = 0;
  42.    APIRET rc = NO_ERROR;
  43.    HAB hAB = NULLHANDLE;
  44.    HMQ hMQ = NULLHANDLE;
  45.    HWND hwndFrame = NULLHANDLE;
  46.    HWND hwndClient = NULLHANDLE;
  47.    BOOL fSuccess = FALSE;
  48.    PTHREADDATA threadData = NULL;
  49.    char default_log[] = "debug terminal.log";
  50.    char *pLog = default_log;
  51.  
  52.    if(argc < 2)
  53.    {
  54.       return 1;
  55.    }
  56.  
  57.    privateStore = NULL;
  58.  
  59.    rc = DosExitList(EXLST_ADD, cleanup);
  60.    if(rc == NO_ERROR)
  61.    {
  62.       PVOID pAlloc = NULL;
  63.       HFILE hFile = NULLHANDLE;
  64.       ULONG fsOpenFlags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
  65.       ULONG fsOpenMode = OPEN_SHARE_DENYNONE | OPEN_ACCESS_WRITEONLY;
  66.       ULONG ulAction = 0;
  67.  
  68.       rc = DosAllocMem(&pAlloc, sizeof(THREADDATA), PAG_READ | PAG_WRITE | PAG_COMMIT);
  69.       if(rc == NO_ERROR)
  70.       {
  71.          threadData = pAlloc;
  72.  
  73.          privateStore = threadData;
  74.       }
  75.  
  76.       /* Open&Replace/Create debug file */
  77.       if(threadData && argc > 2)
  78.       {
  79.          pLog = argv[2];
  80.       }
  81.  
  82.       rc = DosOpen(pLog, &hFile, &ulAction, 0UL, FILE_NORMAL, fsOpenFlags, fsOpenMode, (PEAOP2)NULL);
  83.       if(rc == NO_ERROR)
  84.       {
  85.          threadData->hfLogFile = hFile;
  86.          threadData->fLogFile = TRUE;
  87.       }
  88.    }
  89.  
  90.    if(threadData == NULL)
  91.    {
  92.       return 1;
  93.    }
  94.  
  95.    hAB = WinInitialize(0UL);
  96.  
  97.    if(hAB)
  98.    {
  99.       fSuccess = registerTerminalWindow(hAB);
  100.    }
  101.  
  102.    if(fSuccess)
  103.    {
  104.       hMQ = WinCreateMsgQueue(hAB, 0L);
  105.    }
  106.  
  107.    if(hMQ)
  108.    {
  109.       hwndFrame = createFrameWindow(hAB);
  110.    }
  111.  
  112.    if(hwndFrame)
  113.    {
  114.       hwndClient = createClientWindow(hwndFrame, argc, argv);
  115.    }
  116.  
  117.    if(hwndClient)
  118.    {
  119.       QMSG qmsg = { 0 };
  120.       SWP swp = { 0 };
  121.       ULONG cbSWP = sizeof(swp);
  122.  
  123.       if(PrfQueryProfileData(HINI_USERPROFILE, "PMDebugTerminal", "Window Position", &swp, &cbSWP))
  124.       {
  125.          WinSetWindowPos(hwndFrame, HWND_TOP, swp.x, swp.y, swp.cx, swp.cy, SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE);
  126.       }
  127.       else
  128.       {
  129.          WinShowWindow(hwndFrame, TRUE);
  130.       }
  131.  
  132.       while(WinGetMsg(hAB, &qmsg, (HWND)NULLHANDLE, 0UL, 0UL))
  133.       {
  134.          WinDispatchMsg(hAB, &qmsg);
  135.       }
  136.    }
  137.  
  138.    if(hAB)
  139.    {
  140.       if(hMQ)
  141.       {
  142.          if(hwndFrame)
  143.          {
  144.             WinDestroyWindow(hwndFrame);
  145.          }
  146.          WinDestroyMsgQueue(hMQ);
  147.       }
  148.       WinTerminate(hAB);
  149.    }
  150.  
  151.    return iRet;
  152. }
  153.  
  154.  
  155. static HWND _Optlink createFrameWindow(HAB hAB)
  156. {
  157.    LONG lLength = 0;
  158.    HWND hwndFrame = NULLHANDLE;
  159.    char pszTitle[256] = "";
  160.  
  161.    if((lLength = WinLoadString(hAB, (HMODULE)NULLHANDLE, IDS_APPTITLE, sizeof(pszTitle), pszTitle)) != 0L)
  162.    {
  163.       FRAMECDATA fcd = { 0 };
  164.  
  165.       fcd.cb = sizeof(fcd);
  166.       fcd.flCreateFlags = FCF_SIZEBORDER | FCF_SHELLPOSITION | FCF_TASKLIST | FCF_TITLEBAR | FCF_SYSMENU | FCF_MINMAX | FCF_HORZSCROLL | FCF_VERTSCROLL | FCF_MENU;
  167.       fcd.hmodResources = NULLHANDLE;
  168.       fcd.idResources = WIN_APPFRAME;
  169.       hwndFrame = WinCreateWindow(HWND_DESKTOP, WC_FRAME, pszTitle, 0UL, 0L, 0L, 0L, 0L, (HWND)NULLHANDLE, HWND_TOP, fcd.idResources, &fcd, NULL);
  170.    }
  171.    return hwndFrame;
  172. }
  173.  
  174.  
  175. static HWND _Optlink createClientWindow(HWND hwndFrame, int argc, char *argv[])
  176. {
  177.    HWND hwndClient = NULLHANDLE;
  178.    TERMCDATA tcd = { 0 };
  179.    PTHREADDATA threadData = privateStore;
  180.  
  181.    tcd.cb = sizeof(tcd);
  182.    tcd.rows = 1000;
  183.    tcd.columns = 132;
  184.    tcd.hDebugPipe = (HFILE)atol(argv[1]);
  185.    tcd.fLogFile = threadData->fLogFile;
  186.    tcd.hfLogFile = threadData->hfLogFile;
  187.  
  188.    hwndClient = WinCreateWindow(hwndFrame, WC_TERMINALWINDOW, NULL, 0UL, 0L, 0L, 0L, 0L, (HWND)hwndFrame, HWND_TOP, FID_CLIENT, &tcd, NULL);
  189.  
  190.    return hwndClient;
  191. }
  192.  
  193.  
  194. VOID APIENTRY cleanup(ULONG ulVal)
  195. {
  196.    PTHREADDATA threadData = privateStore;
  197.  
  198.    if(threadData)
  199.    {
  200.       if(threadData->fLogFile)
  201.       {
  202.          DosClose(threadData->hfLogFile);
  203.       }
  204.       DosFreeMem(threadData);
  205.    }
  206.  
  207.    DosExitList(EXLST_EXIT, (PVOID)NULL);
  208.  
  209.    return;
  210. }
  211.