home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / Debugger / cordbg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.6 KB  |  132 lines

  1.  
  2. #include "stdafx.h"
  3.  
  4. #include "dshell.h"
  5.  
  6. #ifdef _INTERNAL_DEBUG_SUPPORT_
  7. #include "__file__.ver"
  8. #else
  9. #include "__file__.h"
  10. #endif
  11. #include "corver.h"
  12.  
  13. int _cdecl wmain(int argc, WCHAR *argv[])
  14. {
  15.     DebuggerShell *shell;
  16.  
  17.     // Ensure that cordbg will work with remote. Remote doesn't like
  18.     // buffered output, so we remove the default buffer from stdout
  19.     // with this call.
  20.     setbuf(stdout, NULL);
  21.     
  22.     shell = new DebuggerShell(stdin, stdout);
  23.  
  24.     if (shell == NULL)
  25.     {
  26.         fprintf(stderr, "Initialization failed. Reason: out of memory.\n");
  27.         return (-1);
  28.     }
  29.  
  30. #ifdef _INTERNAL_DEBUG_SUPPORT_
  31.     shell->Write(L"%s.  Version %s", VER_FILEDESCRIPTION_WSTR, VER_FILEVERSION_WSTR);
  32. #else
  33.     shell->Write(L"%s. ", VER_FILEDESCRIPTION_WSTR);
  34. #endif
  35.  
  36.     shell->Write(L"\n%s\n\n", VER_LEGALCOPYRIGHT_DOS_STR);
  37.  
  38.     if ((argc > 1) && ((argv[1][0] == '/' || argv[1][0] == '-') && argv[1][1] == '?'))
  39.     {
  40.         shell->Write(L"Usage:  CORDBG [<program name> [<program args>]] [<CorDbg optional args>]\n");   
  41.         
  42.         delete shell;
  43.         return (0);
  44.     }
  45.     
  46.     HRESULT hr = shell->Init();
  47.  
  48.     if (SUCCEEDED(hr))
  49.     {
  50.         //
  51.         // Process command line arguments
  52.         //
  53.         if (argc > 1)
  54.         {
  55.             int cmdLen = 1;  // Start at one to handle the null char
  56.  
  57.             if (argv[1][0] != '!')
  58.             {
  59.                 cmdLen += 5;  // Implied "!run " command
  60.             }
  61.  
  62.             for (int i = 1; i < argc; i++)
  63.             {
  64.                 cmdLen += wcslen(argv[i]) + 1;  // Add one to handle space between args
  65.             }
  66.  
  67.             // Allocate the string on the stack
  68.             WCHAR *command = (WCHAR *) _alloca(cmdLen * sizeof(WCHAR));
  69.             command[0] = L'\0';
  70.  
  71.             if (argv[1][0] != L'!')
  72.             {
  73.                 wcscpy(command, L"!run ");  // Implied "!run " command
  74.             }
  75.  
  76.             for (WCHAR **arg = &(argv[1]), **end = &(argv[0]) + argc; arg < end; arg++)
  77.             {
  78.                 wcscat(command, *arg);
  79.                 if (arg + 1 != end)
  80.                 {
  81.                     wcscat(command, L" ");
  82.                 }
  83.             }
  84.  
  85.             //
  86.             // Go through the command line and execute the various commands
  87.             //
  88.             for (WCHAR *cmd = NULL, *ptr = command; *ptr != L'\0'; ptr++)
  89.             {
  90.                 if (*ptr == L'!' || *(ptr + 1) == L'\0')
  91.                 {
  92.                     // Overwrite '!' with null char to terminate current command
  93.                     if (*ptr == L'!')
  94.                     {
  95.                         *ptr = L'\0';
  96.  
  97.                         // Get rid of trailing spaces on commands
  98.                         if (ptr > command)
  99.                         {
  100.                             *(ptr-1) = L'\0';
  101.                         }
  102.                     }
  103.  
  104.                     // If we've reached the end of a command, execute it
  105.                     if (cmd != NULL)
  106.                     {
  107.                         shell->Write(shell->GetPrompt());
  108.                         shell->Write(L" %s\n", cmd);
  109.                         shell->DoCommand(cmd);
  110.                     }
  111.  
  112.                     // Save the beginning of the next command
  113.                     cmd = ptr + 1;
  114.                 }
  115.             }
  116.         }
  117.  
  118.         // Read commands from the user prompt
  119.         while (!shell->m_quit)
  120.             shell->ReadCommand();
  121.     }
  122.     else
  123.     {
  124.         shell->Write(L"Initialization failed. Reason: ");
  125.         shell->ReportError(hr);
  126.     }
  127.  
  128.     delete shell;
  129.  
  130.     return (0);
  131. }
  132.