1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* Windows only app to show a modal debug dialog - launched by nsDebug.cpp */
8 #include <windows.h>
9 #include <stdlib.h>
10 #ifdef _MSC_VER
11 #  include <strsafe.h>
12 #endif
13 #ifdef __MINGW32__
14 /* MingW currently does not implement a wide version of the
15    startup routines.  Workaround is to implement something like
16    it ourselves.  See bug 472063 */
17 #  include <stdio.h>
18 #  include <shellapi.h>
19 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
20 
21 #  undef __argc
22 #  undef __wargv
23 
24 static int __argc;
25 static wchar_t** __wargv;
26 
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCommandLine,int nCmdShow)27 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
28                    LPSTR lpszCommandLine, int nCmdShow) {
29   LPWSTR commandLine = GetCommandLineW();
30 
31   /* parse for __argc and __wargv for compatibility, since mingw
32    * doesn't claim to support it :(
33    */
34   __wargv = CommandLineToArgvW(commandLine, &__argc);
35   if (!__wargv) return 127;
36 
37   /* need to strip off any leading whitespace plus the first argument
38    * (the executable itself) to match what should be passed to wWinMain
39    */
40   while ((*commandLine <= L' ') && *commandLine) {
41     ++commandLine;
42   }
43   if (*commandLine == L'"') {
44     ++commandLine;
45     while ((*commandLine != L'"') && *commandLine) {
46       ++commandLine;
47     }
48     if (*commandLine) {
49       ++commandLine;
50     }
51   } else {
52     while (*commandLine > L' ') {
53       ++commandLine;
54     }
55   }
56   while ((*commandLine <= L' ') && *commandLine) {
57     ++commandLine;
58   }
59 
60   int result = wWinMain(hInstance, hPrevInstance, commandLine, nCmdShow);
61   LocalFree(__wargv);
62   return result;
63 }
64 #endif /* __MINGW32__ */
65 
wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpszCmdLine,int nCmdShow)66 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
67                     LPWSTR lpszCmdLine, int nCmdShow) {
68   /* support for auto answering based on words in the assertion.
69    * the assertion message is sent as a series of arguements (words) to the
70    * commandline. set a "word" to 0xffffffff to let the word not affect this
71    * code. set a "word" to 0xfffffffe to show the dialog. set a "word" to 0x5 to
72    * ignore (program should continue). set a "word" to 0x4 to retry (should fall
73    * into debugger). set a "word" to 0x3 to abort (die).
74    */
75   DWORD regType;
76   DWORD regValue = -1;
77   DWORD regLength = sizeof regValue;
78   HKEY hkeyCU, hkeyLM;
79   RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\mozilla.org\\windbgdlg", 0,
80                 KEY_READ, &hkeyCU);
81   RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\mozilla.org\\windbgdlg", 0,
82                 KEY_READ, &hkeyLM);
83   for (int i = __argc - 1; regValue == (DWORD)-1 && i; --i) {
84     bool ok = false;
85     if (hkeyCU)
86       ok = RegQueryValueExW(hkeyCU, __wargv[i], 0, &regType, (LPBYTE)&regValue,
87                             &regLength) == ERROR_SUCCESS;
88     if (!ok && hkeyLM)
89       ok = RegQueryValueExW(hkeyLM, __wargv[i], 0, &regType, (LPBYTE)&regValue,
90                             &regLength) == ERROR_SUCCESS;
91     if (!ok) regValue = -1;
92   }
93   if (hkeyCU) RegCloseKey(hkeyCU);
94   if (hkeyLM) RegCloseKey(hkeyLM);
95   if (regValue != (DWORD)-1 && regValue != (DWORD)-2) return regValue;
96   static const int size = 4096;
97   static WCHAR msg[size];
98 
99 #ifdef _MSC_VER
100   StringCchPrintfW(msg,
101 #else
102   snwprintf(msg,
103 #endif
104                    size,
105                    L"%s\n\nClick Abort to exit the Application.\n"
106                    L"Click Retry to Debug the Application.\n"
107                    L"Click Ignore to continue running the Application.",
108                    lpszCmdLine);
109   msg[size - 1] = L'\0';
110   return MessageBoxW(
111       nullptr, msg, L"NSGlue_Assertion",
112       MB_ICONSTOP | MB_SYSTEMMODAL | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3);
113 }
114