home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / cmdwnd / msdevcmd / cmdedit.cpp < prev    next >
C/C++ Source or Header  |  1998-04-08  |  24KB  |  1,033 lines

  1. // CmdEdit.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "msdevcmd.h"
  6. #include "CmdEdit.h"
  7. #include "io.h"
  8. #include <objmodel\dbgdefs.h>
  9. #include "CommandWindow.h"
  10. #include "msdevcmddlg.h"
  11.  
  12. #include <direct.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #pragma warning( disable : 4310 )  // Disable warning message: warning C4310: cast truncates constant value
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CCmdEdit
  26.  
  27. CCmdEdit::CCmdEdit()
  28. {
  29.     CString strCurr;
  30.     UINT nID;
  31.  
  32.     // Command initialization
  33.     m_rgCmds.SetAt(_T("?"), CmdHelp);
  34.     m_rgCmds.SetAt(_T("Open"), CmdOpen);
  35.     m_rgCmds.SetAt(_T("Close"), CmdClose);
  36.     m_rgCmds.SetAt(_T("q"), CmdQuit);
  37.     m_rgCmds.SetAt(_T("G"), CmdGo);
  38.     m_rgCmds.SetAt(_T("s"), CmdStep);
  39.     m_rgCmds.SetAt(_T("t"), CmdStepInto);
  40.     m_rgCmds.SetAt(_T("bld"), CmdBuild);
  41.     m_rgCmds.SetAt(_T("rb"), CmdRebuild);
  42.     m_rgCmds.SetAt(_T("cd"), CmdCD);
  43.     m_rgCmds.SetAt(_T("cls"), CmdCls);
  44.     m_rgCmds.SetAt(_T("!"),  CmdDos);
  45.     m_rgCmds.SetAt(_T("bl"), CmdBL );
  46.     m_rgCmds.SetAt(_T("bc"), CmdBC );
  47.     m_rgCmds.SetAt(_T("bp"), CmdBP );
  48.     m_rgCmds.SetAt(_T("bd"), CmdBD );
  49.     m_rgCmds.SetAt(_T("be"), CmdBE );
  50.  
  51.     for (nID = IDS_START; strCurr.LoadString(nID); nID++)
  52.     {
  53.         m_rgStrings.AddTail(strCurr);
  54.     }
  55.  
  56.     // other initialization
  57.     m_pDlgParent = NULL;
  58.  
  59.     // determine if we are running on Win95/98 or WinNT
  60.     DWORD dwVersion = ::GetVersion();
  61.  
  62.     m_fWin4 = LOBYTE(dwVersion) > 3;
  63.     m_fWin95 = m_fWin4 && (dwVersion & 0x80000000) != 0;
  64.  
  65. }
  66.  
  67. CCmdEdit::~CCmdEdit()
  68. {
  69. }
  70.  
  71.  
  72. void CCmdEdit::SetParent(CMsdevcmdDlg *pParent)
  73. {
  74.     m_pDlgParent = pParent;
  75. }
  76.  
  77.  
  78. BOOL CCmdEdit::FHaveApp()
  79. {
  80.     CCommandWindow *pCmdWnd;
  81.     
  82.     pCmdWnd = theApp.GetCmd();
  83.     if (pCmdWnd)
  84.     {
  85.         m_spApplication = pCmdWnd->GetApp();
  86.     }
  87.     return(m_spApplication != NULL);
  88. }
  89.  
  90.  
  91.  
  92. void CCmdEdit::Append(LPCTSTR szText)
  93. {
  94.     CString strOut;
  95.  
  96.     strOut = _T("\r\n");
  97.     strOut += szText;
  98.     SetSel(-1, -1, TRUE);
  99.     ReplaceSel(strOut);
  100. }
  101.  
  102.  
  103.  
  104. BEGIN_MESSAGE_MAP(CCmdEdit, CEdit)
  105.     //{{AFX_MSG_MAP(CCmdEdit)
  106.     ON_WM_CHAR()
  107.     //}}AFX_MSG_MAP
  108. END_MESSAGE_MAP()
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CCmdEdit message handlers
  112.  
  113. void CCmdEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  114. {
  115.     CmdOp pCmd;
  116.  
  117.     if (nChar == _T('\n') || nChar == _T('\r'))
  118.     {
  119.         //process command.
  120.         int iLine, cb;
  121.         TCHAR *pch;
  122.         
  123.         pch = m_strCmd.GetBuffer(_MAX_PATH);
  124.         iLine = LineFromChar(-1);
  125.         cb = GetLine(iLine, pch, _MAX_PATH);
  126.         pch[cb] = _T('\0');
  127.         m_strCmd.ReleaseBuffer();
  128.         if (!FParseCommand(m_strCmd, pCmd))
  129.         {
  130.             if (pCmd == CmdDos || !FSendCommandToMSDEV(m_strCmd))
  131.             {
  132.                 SendCmdToDos(m_strCmd);
  133.             }
  134.         }
  135.          // don't need this if system call is changed to CreateProcess or _pipe.
  136.         if (m_pDlgParent)
  137.             m_pDlgParent->SetForegroundWindow();
  138.     }
  139.     
  140.     CEdit::OnChar(nChar, nRepCnt, nFlags);
  141. }
  142.  
  143.  
  144. void CCmdEdit::SendCmdToDos(CString& rstrCmd)
  145. {
  146.     CString strTmp;
  147.     CStdioFile fileTmp;
  148.     CString strTmpFileName;
  149.     CString strRes;
  150.  
  151.     strTmpFileName = _T("finXXXX");
  152.     _tmktemp(strTmpFileName.GetBuffer(0));
  153.     strTmpFileName.ReleaseBuffer();
  154.     strTmpFileName += _T(".tmp");
  155.  
  156.     strTmp = rstrCmd;
  157.     strTmp += _T(" > ");
  158.     strTmp += strTmpFileName;
  159.     if (!m_fWin95) // nt supports error redirection; win95/8 do not
  160.     {
  161.         strTmp += _T("  2>&1 ");
  162.     }
  163.     try
  164.     {
  165.         ::system(strTmp); // consider changing this to either CreateProcess or _pipe.
  166.         if (fileTmp.Open(strTmpFileName, CFile::modeRead))
  167.         {
  168.             
  169.             while (fileTmp.ReadString(strRes))
  170.             {
  171.                 Append(strRes);
  172.             }
  173.             Append(_T("\0"));
  174.             
  175.             fileTmp.Close();
  176.             _tremove(strTmpFileName);
  177.         }
  178.     }
  179.     catch(...)
  180.     {
  181.     }
  182.  
  183.  
  184. }
  185.  
  186.  
  187. BOOL CCmdEdit::FSendCommandToMSDEV(CString& rstrCmd)
  188. {
  189.     BOOL fOK = FALSE;
  190.     HRESULT hr;
  191.  
  192.     if (FHaveApp())
  193.     {
  194.         CComBSTR bstrCmd = rstrCmd;
  195.  
  196.         hr = m_spApplication->ExecuteCommand(bstrCmd);
  197.         fOK = SUCCEEDED(hr);
  198.     }
  199.     return(fOK);
  200. }
  201.  
  202.  
  203. BOOL CCmdEdit::FParseCommand(CString& rstrCmd, CmdOp& rpCmd)
  204. {
  205.     BOOL fParsed = FALSE;
  206.     POSITION p;
  207.     CString strCmd = rstrCmd;
  208.     CString strCurr;
  209.     CString strMatch;
  210.     int i, j, iMatch;
  211.     LPVOID pCmd;
  212.  
  213.     rpCmd = NULL;
  214.     strCmd.TrimLeft();
  215.     p = m_rgStrings.GetHeadPosition();
  216.     while (p && !fParsed)
  217.     {
  218.         strCurr = m_rgStrings.GetNext(p);
  219.         i = strCurr.Find(_T(';'));
  220.         _ASSERTE(i>0);
  221.         if (i > 0)
  222.         {
  223.             strMatch = strCurr.Left(i);
  224.             iMatch = strCmd.Find(strMatch, 0);
  225.             if (iMatch == 0)
  226.             {
  227.                 // found command.
  228.                 j = strCurr.Find(_T(';'), i+1);
  229.                 strMatch = strCurr.Mid(i+1, j - i - 1);
  230.                 pCmd = NULL;
  231.                 m_rgCmds.Lookup(strMatch, pCmd);
  232.                 if (pCmd)
  233.                 {
  234.                     fParsed = ((CmdOp)pCmd)(this, strMatch, strCmd);
  235.                     rpCmd = (CmdOp)pCmd;
  236.                 }
  237.             }
  238.         }
  239.     }
  240.  
  241.     return(fParsed);
  242. }
  243.  
  244. int CCmdEdit::GetBrkPntList(CStringArray &rgStrBrks)
  245. {
  246.     long cBrks = 0;
  247.     long cBrkPnts = 0;
  248.     HRESULT hr;
  249.  
  250.     if (FHaveApp())
  251.     {
  252.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  253.         CComPtr<IDispatch> pDispDebugger;
  254.         m_spApplication->get_Debugger(&pDispDebugger);
  255.         pDebugger = pDispDebugger;
  256.         if (pDebugger)
  257.         {
  258.             CComQIPtr<IBreakpoints, &IID_IBreakpoints> pBrkPnts;
  259.             CComPtr<IDispatch> pDispBrkPnts;
  260.  
  261.             pDebugger->get_Breakpoints(&pDispBrkPnts);
  262.             pBrkPnts = pDispBrkPnts;
  263.             if (pBrkPnts)
  264.             {
  265.                 hr = pBrkPnts->get_Count(&cBrks);
  266.                 rgStrBrks.RemoveAll();
  267.                 rgStrBrks.Add(_T("# : [*=enabled] File(line) Function, Executable, Type"));
  268.                 while (cBrkPnts < cBrks)
  269.                 {
  270.                     CComPtr<IDispatch> pDispBrkPnt;
  271.                     CComQIPtr<IBreakpoint, &IID_IBreakpoint> pBrkPnt;
  272.                     CComBSTR bstrLocation;
  273.                     CComBSTR bstrFile;
  274.                     CComBSTR bstrFunction;
  275.                     //long lElements;
  276.                     short sEnabled;
  277.                     //DsWindowsMessage dsWndMsg;
  278.                     //long lPassCount;
  279.                     CComBSTR bstrExecutable;
  280.                     DsBreakpointType lType;
  281.                     CComBSTR bstrWndProc;
  282.                     CComVariant varItem = cBrkPnts;
  283.                     /* Type:
  284.                     dsChangedExpression
  285.                     dsLocation
  286.                     dsLocationWithTrueExpression
  287.                     dsLocationWithChangedExpression
  288.                     dsMessage
  289.                     dsTrueExpression  
  290.                     */
  291.  
  292.                     hr = pBrkPnts->Item(varItem, &pDispBrkPnt);
  293.                     pBrkPnt = pDispBrkPnt;
  294.  
  295.                     hr = pBrkPnt->get_Enabled(&sEnabled);
  296.                     CString strOut, strLocation, strType;
  297.                     TCHAR chEnabled = _T(' ');
  298.                     CComBSTR bstrT ;
  299.                     hr = pBrkPnt->get_Type((long *)&lType);
  300.                     hr = pBrkPnt->get_File(&bstrFile);
  301.                     hr = pBrkPnt->get_Function(&bstrFunction);
  302.                     hr = pBrkPnt->get_Executable(&bstrExecutable);
  303.                     hr = pBrkPnt->get_Location(&bstrLocation);
  304.  
  305.                     strLocation = bstrLocation;
  306.                     if (strLocation.GetAt(0) == _T('.'))
  307.                         strLocation.Delete(0); // remove leading "."
  308.                     if (sEnabled)
  309.                     {
  310.                         chEnabled = _T('*');
  311.                     }
  312.                     switch(lType)
  313.                     {
  314.                     case dsChangedExpression:
  315.                         strType.LoadString(IDS_CHANGED_EXPRESSION);
  316.                         break;
  317.                     case dsLocation:
  318.                         strType.LoadString(IDS_LOCATION);
  319.                         break;
  320.                     case dsLocationWithTrueExpression:
  321.                         strType.LoadString(IDS_LOCATIONWITHTRUEEXPRESSION);
  322.                         break;
  323.                     case dsLocationWithChangedExpression:
  324.                         strType.LoadString(IDS_LOCATIONWITHCHANGEDEXPRESSION);
  325.                         break;
  326.                     case dsMessage:
  327.                         strType.LoadString(IDS_MESSAGE);
  328.                         break;
  329.                     case dsTrueExpression:
  330.                         strType.LoadString(IDS_TRUEEXPRESSION);
  331.                         break;
  332.                     }
  333.                     strOut.Format(_T("%2d : %c %ls(%s): %ls, %ls, %s:"),cBrkPnts, chEnabled, bstrFile, strLocation, bstrFunction, bstrExecutable, strType);
  334.  
  335.                     rgStrBrks.Add(strOut);
  336.                     
  337.                     cBrkPnts++;
  338.                 }
  339.             }
  340.         }
  341.     }
  342.     return(cBrkPnts);
  343. }
  344.  
  345.  
  346. HRESULT CCmdEdit::FindDoc(LPCTSTR szFile, CComPtr<IGenericDocument>& pDoc, BOOL fOkToOpen /*= TRUE*/)
  347. {
  348.     HRESULT hr = E_FAIL;
  349.     CComPtr<IDispatch> pDispDocuments;
  350.     CComQIPtr<IDocuments, &IID_IDocuments> pDocuments;
  351.     CComBSTR bstrFile;
  352.     m_spApplication->get_Documents(&pDispDocuments);
  353.     pDocuments = pDispDocuments;
  354.     pDispDocuments = NULL;
  355.     BOOL fFound = FALSE;
  356.  
  357.     if (pDocuments)
  358.     {    // ENSURE DOC IS OPEN -- WE KEEP FILES AROUND THAT MAY HAVE BEEN CLOSED
  359.         CComVariant vtDocType = _T("Auto");
  360.         CComVariant vtBoolReadOnly = FALSE;
  361.         CComPtr<IDispatch> pDispDoc;
  362.         bstrFile = szFile;
  363.         CComQIPtr<IGenericDocument, &IID_IGenericDocument> pDocument;
  364.         if (fOkToOpen)
  365.         {
  366.             hr = pDocuments->Open(bstrFile, vtDocType, vtBoolReadOnly, &pDispDoc);
  367.             pDocument = pDispDoc;
  368.             if (SUCCEEDED(hr) && pDocument)
  369.             {
  370.                 fFound = TRUE;
  371.                 pDoc = pDocument;
  372.             }
  373.         }
  374.         if (!fFound)
  375.         {
  376.             CComPtr<IUnknown> pUnk;
  377.             CComQIPtr<IEnumVARIANT, &IID_IEnumVARIANT> pNewEnum;
  378.             if (SUCCEEDED(pDocuments->get__NewEnum(&pUnk)) && pUnk != NULL)
  379.             {
  380.                 pNewEnum = pUnk;
  381.                 VARIANT varGenericDocument;
  382.  
  383.                 while (!fFound && pNewEnum->Next(1, &varGenericDocument, NULL) == S_OK)
  384.                 {
  385.                     CComQIPtr<IGenericDocument, &IID_IGenericDocument> pGenericDocument;
  386.                     ASSERT (varGenericDocument.vt == VT_DISPATCH);
  387.                     pGenericDocument = varGenericDocument.pdispVal;
  388.                     VariantClear(&varGenericDocument);
  389.                     CComBSTR bstrFullName;
  390.                     pGenericDocument->get_FullName(&bstrFullName);
  391.                     if (bstrFullName == szFile)
  392.                     {
  393.                         fFound = TRUE;
  394.                         pDoc = pGenericDocument;
  395.                     }
  396.                 }
  397.             }
  398.  
  399.  
  400.         }
  401.     }
  402.  
  403.     return(hr);
  404. }
  405.  
  406.  
  407. //================================================================================
  408. // COMMANDS
  409. // Use the following as a template:
  410.  
  411. //BOOL CCmdEdit::CmdGo(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  412. //{
  413. //    BOOL fOK = FALSE;
  414. //
  415. //    return(fOK);
  416. //}
  417.  
  418.  
  419. BOOL CCmdEdit::CmdCD(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  420. {
  421.     BOOL fOK = TRUE;
  422.  
  423.     CString strDir;
  424.     int i;
  425.  
  426.     strDir = rstrCmd;
  427.     i = strDir.Find(_T(' '));
  428.     if (i > 0)
  429.     {
  430.         strDir = strDir.Mid(i+1);
  431.         ::_tchdir(strDir);
  432.     }
  433.     ::_tgetcwd(strDir.GetBuffer(_MAX_PATH), _MAX_PATH);
  434.     strDir.ReleaseBuffer();
  435.     strDir = _T(" --> ") + strDir;
  436.     pThis->Append(strDir);
  437.     if (pThis->FHaveApp())
  438.     {
  439.         CComBSTR bstrDir = strDir;
  440.         pThis->m_spApplication->put_CurrentDirectory(bstrDir);
  441.     }
  442.     return(fOK);
  443. }
  444.  
  445.  
  446. BOOL CCmdEdit::CmdCls(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  447. {
  448.     BOOL fOK = TRUE;
  449.  
  450.     CString strOut;
  451.  
  452.     strOut = _T("\0");
  453.     pThis->SetSel(0, -1, TRUE);
  454.     pThis->ReplaceSel(strOut);
  455.     return(fOK);
  456. }
  457.  
  458.  
  459. BOOL CCmdEdit::CmdHelp(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  460. {
  461.     BOOL fOK = TRUE;
  462.     POSITION p;
  463.     CString strCmd;
  464.     CString strCurr;
  465.     CString strMatch;
  466.     CString strKey;
  467.     LPVOID pCmd;
  468.     int i, j;
  469.     BOOL fShowOnlyEnabled = TRUE;
  470.     BOOL fShow;
  471.  
  472.     strCmd.TrimRight();
  473.     if (strCmd.Find(_T('*')) > 0)
  474.     {
  475.         fShowOnlyEnabled = FALSE;
  476.     }
  477.     p = pThis->m_rgStrings.GetHeadPosition();
  478.     while (p)
  479.     {
  480.         strCmd.Empty();
  481.         fShow = TRUE;
  482.         strCurr = pThis->m_rgStrings.GetNext(p);
  483.         i = strCurr.Find(_T(';'));
  484.         _ASSERTE(i>0);
  485.         if (i > 0)
  486.         {
  487.             strMatch = strCurr.Left(i);
  488.             j = strCurr.Find(_T(';'), i+1);
  489.             strKey = strCurr.Mid(i+1, j - i - 1);
  490.             pCmd = NULL;
  491.             pThis->m_rgCmds.Lookup(strKey, pCmd);
  492.             if (!fShowOnlyEnabled)
  493.             {
  494.                 if (pCmd)
  495.                 {
  496.                     strCmd += _T('*'); // command has handler
  497.                 }
  498.                 else
  499.                 {
  500.                     strCmd += _T(' '); // not enabled/found
  501.                     fShow = !fShowOnlyEnabled;
  502.                 }
  503.             }
  504.             if (fShow)
  505.             {
  506.                 strCmd += strMatch;
  507.                 strCmd += _T(" - ");
  508.                 strCmd += strCurr.Mid(j+1);
  509.                 pThis->Append(strCmd);
  510.             }
  511.         }
  512.     }
  513.  
  514.     int nId = IDS_DEFAULT;
  515.     while (strCmd.LoadString(nId))
  516.     {
  517.         pThis->Append(strCmd);
  518.         nId++;
  519.  
  520.     }
  521.     return(fOK);
  522. }
  523.  
  524. BOOL CCmdEdit::CmdGo(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  525. {
  526.     BOOL fOK = FALSE;
  527.     HRESULT hr;
  528.     if (pThis->FHaveApp())
  529.     {
  530.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  531.         CComPtr<IDispatch> pDispDebugger;
  532.         pThis->m_spApplication->get_Debugger(&pDispDebugger);
  533.         pDebugger = pDispDebugger;
  534.         if (pDebugger)
  535.         {
  536.             hr = pDebugger->Go();
  537.             fOK = TRUE;
  538.         }
  539.     }
  540.     return(fOK);
  541. }
  542.  
  543.  
  544. BOOL CCmdEdit::CmdOpen(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  545. {
  546.     BOOL fOK = FALSE;
  547.     HRESULT hr;
  548.     if (pThis->FHaveApp())
  549.     {
  550.         CString strFile;
  551.         int i;
  552.  
  553.         strFile = rstrCmd;
  554.         i = strFile.Find(_T(' '));
  555.         if (i > 0)
  556.         {
  557.             strFile = strFile.Mid(i);
  558.             strFile.TrimLeft();
  559.             strFile.TrimRight();
  560.             CComQIPtr<IDocuments, &IID_IDocuments> pDocs;
  561.             CComPtr<IDispatch> pDispDocs;
  562.             pThis->m_spApplication->get_Documents(&pDispDocs);
  563.             pDocs = pDispDocs;
  564.  
  565.             if (pDocs)
  566.             {
  567.                 CComVariant vtDocType = _T("Auto");
  568.                 CComVariant vtBoolReadOnly = FALSE;
  569.                 CComBSTR bstrFile = strFile;
  570.                 CComPtr<IDispatch> pDispDoc;
  571.  
  572.                 hr = pDocs->Open(bstrFile, vtDocType, vtBoolReadOnly, &pDispDoc);
  573.                 fOK = TRUE; // assume we succeeded
  574.             }
  575.         }
  576.     }
  577.     return(fOK);
  578. }
  579.  
  580.  
  581. BOOL CCmdEdit::CmdClose(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  582. {
  583.     BOOL fOK = FALSE;
  584.  
  585.     HRESULT hr;
  586.     if (pThis->FHaveApp())
  587.     {
  588.         CComQIPtr<IGenericWindow, &IID_IGenericWindow> pWindow;
  589.         CComPtr<IDispatch> pDispWindow;
  590.         pThis->m_spApplication->get_ActiveWindow(&pDispWindow);
  591.         pWindow = pDispWindow;
  592.         if (pWindow)
  593.         {
  594.             CComVariant varSaveChanges = dsSaveChangesPrompt;
  595.             DsSaveStatus iSaved;
  596.             hr = pWindow->Close(varSaveChanges, &iSaved);
  597.             fOK = TRUE; // assume we succeeded
  598.         }
  599.     }
  600.     return(fOK);
  601. }
  602.  
  603. BOOL CCmdEdit::CmdBuild(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  604. {
  605.     BOOL fOK = FALSE;
  606.  
  607.     HRESULT hr;
  608.     if (pThis->FHaveApp())
  609.     {
  610.         CComVariant varConfig;
  611.         CComPtr<IDispatch> pDispConfig;
  612.  
  613.         hr = pThis->m_spApplication->get_ActiveConfiguration(&pDispConfig);
  614.         varConfig = pDispConfig;
  615.         hr = pThis->m_spApplication->Build(varConfig);
  616.     }
  617.     return(fOK);
  618. }
  619.  
  620. BOOL CCmdEdit::CmdRebuild(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  621. {
  622.     BOOL fOK = FALSE;
  623.  
  624.     HRESULT hr;
  625.     if (pThis->FHaveApp())
  626.     {
  627.         CComVariant varConfig;
  628.         CComPtr<IDispatch> pDispConfig;
  629.  
  630.         hr = pThis->m_spApplication->get_ActiveConfiguration(&pDispConfig);
  631.         varConfig = pDispConfig;
  632.         hr = pThis->m_spApplication->RebuildAll(varConfig);
  633.     }
  634.     return(fOK);
  635. }
  636.  
  637.  
  638.  
  639.  
  640. BOOL CCmdEdit::CmdStep(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  641. {
  642.     BOOL fOK = FALSE;
  643.  
  644.     HRESULT hr;
  645.     if (pThis->FHaveApp())
  646.     {
  647.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  648.         CComPtr<IDispatch> pDispDebugger;
  649.         pThis->m_spApplication->get_Debugger(&pDispDebugger);
  650.         pDebugger = pDispDebugger;
  651.         if (pDebugger)
  652.         {
  653.             hr = pDebugger->StepOver();
  654.             fOK = TRUE;
  655.         }
  656.     }
  657.     return(fOK);
  658. }
  659.  
  660.  
  661. BOOL CCmdEdit::CmdStepInto(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  662. {
  663.     BOOL fOK = FALSE;
  664.  
  665.     HRESULT hr;
  666.     if (pThis->FHaveApp())
  667.     {
  668.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  669.         CComPtr<IDispatch> pDispDebugger;
  670.         pThis->m_spApplication->get_Debugger(&pDispDebugger);
  671.         pDebugger = pDispDebugger;
  672.         if (pDebugger)
  673.         {
  674.             hr = pDebugger->StepInto();
  675.             fOK = TRUE;
  676.         }
  677.     }
  678.     return(fOK);
  679. }
  680.  
  681.  
  682. BOOL CCmdEdit::CmdQuit(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  683. {
  684.     BOOL fOK = TRUE;
  685.     HWND hwnd;
  686.     CWnd *pwndParent;
  687.  
  688.     pwndParent = pThis->GetParent();
  689.     if (pwndParent)
  690.     {
  691.         hwnd = pwndParent->GetSafeHwnd();
  692.         ::SendMessage(hwnd, WM_CLOSE, 0, 0L);
  693.     }
  694.     return(fOK);
  695. }
  696.  
  697.  
  698. BOOL CCmdEdit::CmdExit(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  699. {
  700.     BOOL fOK = TRUE;
  701.  
  702.     HRESULT hr;
  703.     if (pThis->FHaveApp())
  704.     {
  705.         hr = pThis->m_spApplication->Quit();
  706.         fOK = TRUE;
  707.     }
  708.     return(fOK);
  709. }
  710.  
  711.  
  712.  
  713.  
  714.  
  715. BOOL CCmdEdit::CmdDos(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  716. {
  717.     BOOL fOK = FALSE;
  718.     rstrCmd;
  719.  
  720.     // don't do anything but return false. We will check for this one later, and bypass calling msdev.
  721.     return(fOK);
  722. }
  723.  
  724. BOOL CCmdEdit::CmdBL(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  725. {
  726.     BOOL fOK = TRUE;
  727.     CStringArray rgStrBrks;
  728.     int i, iMax;
  729.  
  730.     if (pThis->FHaveApp())
  731.     {
  732.         if (pThis->GetBrkPntList(rgStrBrks) > 0)
  733.         {
  734.             iMax = rgStrBrks.GetUpperBound();
  735.             for (i = 0; i < iMax; i++)
  736.             {
  737.                 pThis->Append(rgStrBrks.GetAt(i));
  738.             }
  739.         }
  740.     }
  741.     return(fOK);
  742. }
  743.  
  744.  
  745.  
  746. BOOL CCmdEdit::CmdBC(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  747. {
  748.     BOOL fOK = TRUE;
  749.     HRESULT hr;
  750.     long cBrks = 0;
  751.     CString strCmd = rstrCmd;
  752.     CString strT;
  753.     int i;
  754.     long lBrkPnt;
  755.  
  756.     if (pThis->FHaveApp())
  757.     {
  758.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  759.         CComPtr<IDispatch> pDispDebugger;
  760.         pThis->m_spApplication->get_Debugger(&pDispDebugger);
  761.         pDebugger = pDispDebugger;
  762.         if (pDebugger)
  763.         {
  764.             CComQIPtr<IBreakpoints, &IID_IBreakpoints> pBrkPnts;
  765.             CComPtr<IDispatch> pDispBrkPnts;
  766.  
  767.             pDebugger->get_Breakpoints(&pDispBrkPnts);
  768.             pBrkPnts = pDispBrkPnts;
  769.             if (pBrkPnts)
  770.             {
  771.                 hr = pBrkPnts->get_Count(&cBrks);
  772.                 strCmd.TrimLeft();
  773.                 i = strCmd.Find(rstrCmdName);
  774.                 _ASSERTE(i >= 0);
  775.                 strCmd = strCmd.Mid(i + rstrCmdName.GetLength());
  776.                 strCmd.TrimLeft();
  777.                 if (strCmd.IsEmpty() || strCmd.Find(_T('*')))
  778.                 { // clear all
  779.                     CComBSTR bstrCommand;
  780.  
  781.                     bstrCommand = _T("DebugRemoveAllBreakpoints");
  782.                     hr = pThis->m_spApplication->ExecuteCommand(bstrCommand);
  783.                 }
  784.                 else
  785.                 {
  786.                     while (!strCmd.IsEmpty())
  787.                     {
  788.                         i = strCmd.Find(_T(','));
  789.                         if (i < 0) // end of line?
  790.                         {
  791.                             lBrkPnt = _ttol(strCmd);
  792.                             strCmd.Empty();
  793.                         }
  794.                         else
  795.                         {
  796.                             strT = strCmd.Left(i);
  797.                             lBrkPnt = _ttol(strT);
  798.                             strCmd = strCmd.Mid(i+1);
  799.                         }
  800.                         
  801.                         CComPtr<IDispatch> pDispBreakpoint;
  802.                         CComQIPtr<IBreakpoint, &IID_IBreakpoint> pBreakpoint;
  803.                         CComVariant varBrkPnt;
  804.                         CComBSTR bstrLine;
  805.                         
  806.                         varBrkPnt = lBrkPnt;
  807.                         hr = pBrkPnts->Item(varBrkPnt, &pDispBreakpoint);
  808.                         pBreakpoint = pDispBreakpoint;
  809.                         if(pBreakpoint)
  810.                         {
  811.                             pBreakpoint->Remove();
  812.                         }
  813.                     }
  814.                 }
  815.  
  816.             }
  817.         }
  818.     }
  819.     return(fOK);
  820. }
  821.  
  822. BOOL CCmdEdit::CmdBP(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  823. { //;Set Breakpoint. Arguments are [File,]"." (current line), line number, ".+n" 
  824.     // where n is an integer offset to line to set breakpoint on. Eg., "bp foo.cpp, 32" or "bp ." or "bp  .+3".
  825.     BOOL fOK = TRUE;
  826.     HRESULT hr;
  827.     if (pThis->FHaveApp())
  828.     {
  829.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  830.         CComPtr<IDispatch> pDispDebugger;
  831.         CComQIPtr<IBreakpoints, &IID_IBreakpoints> pBrkPnts;
  832.         CComPtr<IDispatch> pDispBrkPnts;
  833.         hr = pThis->m_spApplication->get_Debugger(&pDispDebugger);
  834.         pDebugger = pDispDebugger;
  835.         if (pDebugger)
  836.         {
  837.             CString strFile, strCmd, strLine;
  838.             int i, cb;
  839.             long lLine = 0;
  840.             CComPtr<IGenericDocument> pGenericDoc;
  841.  
  842.             strCmd = rstrCmd;
  843.             i = strCmd.Find(rstrCmdName);
  844.             _ASSERTE(i >= 0); // how did we get here if this is false?
  845.             if (i >= 0)
  846.             {
  847.                 strCmd = strCmd.Mid(i + rstrCmdName.GetLength());
  848.                 i = strCmd.Find(_T(','));
  849.                 if (i >= 0)
  850.                 { // have a file, we surmise
  851.                     strFile = strCmd.Left(i);
  852.                     strCmd = strCmd.Mid(i+1);
  853.                     pThis->FindDoc(strFile, pGenericDoc, TRUE);
  854.                     pGenericDoc->put_Active(VARIANT_TRUE);;
  855.                 }
  856.                 else
  857.                 {
  858.                     CComPtr<IDispatch> pDispDoc;
  859.                     pThis->m_spApplication->get_ActiveDocument(&pDispDoc);
  860.                     if (pDispDoc)
  861.                     {
  862.                         pDispDoc.QueryInterface(&pGenericDoc);
  863.                     }
  864.                 }
  865.                 strLine = strCmd.SpanIncluding(_T(" 0123456789."));
  866.                 cb = strLine.GetLength();
  867.                 if (cb > 0)
  868.                 {
  869.                     i = strCmd.Find(strLine);
  870.                     strLine.TrimLeft();
  871.                     if (strLine.GetAt(0) == _T('.'))
  872.                         strLine.Delete(0); // remove leading "."
  873.                     _ASSERTE(i >= 0);
  874.                     strCmd = strCmd.Mid(i + cb);
  875.                     strCmd.TrimLeft();
  876.                     lLine = _ttol(strLine);
  877.                     if (!strCmd.IsEmpty())
  878.                     {
  879.                         CString strT;
  880.                         BOOL fAdd = strCmd.GetAt(0) == _T('+');
  881.                         if (strCmd.GetAt(0) == _T('-') || fAdd)
  882.                         {
  883.                             strCmd = strCmd.Mid(1);
  884.                             strT = strCmd.SpanIncluding(_T(" 0123456789"));
  885.                             strT.TrimLeft();
  886.                             if (fAdd)
  887.                             {
  888.                                 lLine += _ttol(strT);
  889.                             }
  890.                             else
  891.                             {
  892.                                 lLine -= _ttol(strT);
  893.                             }
  894.                             if (lLine < 0)
  895.                                 lLine = 0;
  896.                         }
  897.                     }
  898.                 }
  899.                 pDebugger->get_Breakpoints(&pDispBrkPnts);
  900.                 pBrkPnts = pDispBrkPnts;
  901.                 if (pBrkPnts && pGenericDoc)
  902.                 {
  903.                     CComQIPtr<ITextDocument, &IID_ITextDocument> pTextDoc;
  904.                     CComPtr<IDispatch> pDispBrkPnt;
  905.                     CComPtr<IDispatch> pDispSel;
  906.                     CComQIPtr<ITextSelection, &IID_ITextSelection> pSel;
  907.                     hr = pGenericDoc->put_Active(VARIANT_TRUE);
  908.                     pTextDoc = pGenericDoc;
  909.                     if (pTextDoc)
  910.                     {
  911.                         hr = pTextDoc->get_Selection(&pDispSel);
  912.                         if (SUCCEEDED(hr) && pDispSel)
  913.                         {
  914.                             CComVariant varSelMode = dsMove;
  915.                             CComVariant varSel;
  916.                             pSel = pDispSel;
  917.                             hr = pSel->GoToLine(lLine, varSelMode);
  918.  
  919.                             varSel = lLine;
  920.                             hr = pBrkPnts->AddBreakpointAtLine(varSel, &pDispBrkPnt);
  921.                         }
  922.                     }
  923.                 }
  924.             }
  925.         }
  926.     }
  927.     return(fOK);
  928. }
  929.  
  930.  
  931. BOOL CCmdEdit::CmdBD(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  932. {
  933.     pThis->DoEnableDisableCmds(rstrCmdName, rstrCmd, FALSE);
  934.     return(TRUE);
  935. }
  936.  
  937. BOOL CCmdEdit::CmdBE(CCmdEdit *pThis, CString& rstrCmdName, CString& rstrCmd)
  938. {
  939.     pThis->DoEnableDisableCmds(rstrCmdName, rstrCmd, TRUE);
  940.     return(TRUE);
  941. }
  942.  
  943. BOOL CCmdEdit::DoEnableDisableCmds(CString& rstrCmdName, CString& rstrCmd, BOOL fEnable)
  944. { // enable/disable breakpoints
  945.     BOOL fOK = TRUE;
  946.     HRESULT hr;
  947.     long cBrkPnts = 0;
  948.     long cBrks = 0;
  949.     CString strCmd = rstrCmd;
  950.     int i;
  951.     long lBrkPnt;
  952.     short sEnabled = fEnable ? VARIANT_TRUE : VARIANT_FALSE;
  953.     CString strT;
  954.  
  955.     if (FHaveApp())
  956.     {
  957.         CComQIPtr<IDebugger, &IID_IDebugger> pDebugger;
  958.         CComPtr<IDispatch> pDispDebugger;
  959.         m_spApplication->get_Debugger(&pDispDebugger);
  960.         pDebugger = pDispDebugger;
  961.         if (pDebugger)
  962.         {
  963.             CComQIPtr<IBreakpoints, &IID_IBreakpoints> pBrkPnts;
  964.             CComPtr<IDispatch> pDispBrkPnts;
  965.  
  966.             pDebugger->get_Breakpoints(&pDispBrkPnts);
  967.             pBrkPnts = pDispBrkPnts;
  968.             if (pBrkPnts)
  969.             {
  970.                 hr = pBrkPnts->get_Count(&cBrks);
  971.                 strCmd.TrimLeft();
  972.                 i = strCmd.Find(rstrCmdName);
  973.                 _ASSERTE(i >= 0);
  974.                 strCmd = strCmd.Mid(i + rstrCmdName.GetLength());
  975.                 strCmd.TrimLeft();
  976.                 if (strCmd.IsEmpty() || strCmd.Find(_T('*')) >= 0 )
  977.                 { // enable/disable all
  978.                     for (lBrkPnt = 0; lBrkPnt < cBrks; lBrkPnt++)
  979.                     {
  980.                         CComPtr<IDispatch> pDispBreakpoint;
  981.                         CComQIPtr<IBreakpoint, &IID_IBreakpoint> pBrkPnt;
  982.                         CComVariant varBrkPnt;
  983.                         CComBSTR bstrLine;
  984.                         
  985.                         varBrkPnt = lBrkPnt;
  986.                         hr = pBrkPnts->Item(varBrkPnt, &pDispBreakpoint);
  987.                         pBrkPnt = pDispBreakpoint;
  988.                         if (SUCCEEDED(hr) && pBrkPnt)
  989.                         {
  990.                             hr = pBrkPnt->put_Enabled(sEnabled);
  991.                         }
  992.                     }
  993.                 }
  994.                 else
  995.                 {
  996.                     while (!strCmd.IsEmpty())
  997.                     {
  998.                         i = strCmd.Find(_T(','));
  999.                         if (i < 0) // end of line?
  1000.                         {
  1001.                             lBrkPnt = _ttol(strCmd);
  1002.                             strCmd.Empty();
  1003.                         }
  1004.                         else
  1005.                         {
  1006.                             strT = strCmd.Left(i);
  1007.                             lBrkPnt = _ttol(strT);
  1008.                             strCmd = strCmd.Mid(i+1);
  1009.                         }
  1010.                         
  1011.                         CComPtr<IDispatch> pDispBreakpoint;
  1012.                         CComQIPtr<IBreakpoint, &IID_IBreakpoint> pBrkPnt;
  1013.                         CComVariant varBrkPnt;
  1014.                         CComBSTR bstrLine;
  1015.                         
  1016.                         varBrkPnt = lBrkPnt;
  1017.                         hr = pBrkPnts->Item(varBrkPnt, &pDispBreakpoint);
  1018.                         pBrkPnt = pDispBreakpoint;
  1019.                         if (SUCCEEDED(hr) && pBrkPnt)
  1020.                         {
  1021.                             hr = pBrkPnt->put_Enabled(sEnabled);
  1022.                         }
  1023.  
  1024.                     }
  1025.                 }
  1026.  
  1027.             }
  1028.         }
  1029.     }
  1030.     return(fOK);
  1031. }
  1032.  
  1033.