Bug 787313 - Add `/attach-console` argument on Windows in order to attach Firefox std IO to parent console. r=ted
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 25 Sep 2012 12:58:08 -0400
changeset 108173 e9f40f834e8de005bdbdda0980c967efa6b9ae8f
parent 108172 bbbd07f4cadce95ae8127c6c5d3acf9074dfc191
child 108174 59a16157483a93c604c82987454e6f8a88367cfe
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersted
bugs787313
milestone18.0a1
Bug 787313 - Add `/attach-console` argument on Windows in order to attach Firefox std IO to parent console. r=ted
toolkit/xre/nsNativeAppSupportWin.cpp
--- a/toolkit/xre/nsNativeAppSupportWin.cpp
+++ b/toolkit/xre/nsNativeAppSupportWin.cpp
@@ -327,32 +327,16 @@ NS_INTERFACE_MAP_BEGIN(nsNativeAppSuppor
     NS_INTERFACE_MAP_ENTRY(nsIObserver)
 NS_INTERFACE_MAP_END_INHERITING(nsNativeAppSupportBase)
 
 NS_IMPL_ADDREF_INHERITED(nsNativeAppSupportWin, nsNativeAppSupportBase)
 NS_IMPL_RELEASE_INHERITED(nsNativeAppSupportWin, nsNativeAppSupportBase)
 
 void
 nsNativeAppSupportWin::CheckConsole() {
-    // Try to attach console to the parent process.
-    // It will succeed when the parent process is a command line,
-    // so that stdio will be displayed in it.
-    if (AttachConsole(ATTACH_PARENT_PROCESS)) {
-        // Change std handles to refer to new console handles. Before doing so,
-        // ensure that stdout/stderr haven't been redirected to a valid file
-        if (_fileno(stdout) == -1 || _get_osfhandle(fileno(stdout)) == -1)
-            freopen("CONOUT$", "w", stdout);
-        // There isn't any `CONERR$`, so that we merge stderr into CONOUT$
-        // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
-        if (_fileno(stderr) == -1 || _get_osfhandle(fileno(stderr)) == -1)
-            freopen("CONOUT$", "w", stderr);
-        if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1)
-            freopen("CONIN$", "r", stdin);
-    }
-
     for ( int i = 1; i < gArgc; i++ ) {
         if ( strcmp( "-console", gArgv[i] ) == 0
              ||
              strcmp( "/console", gArgv[i] ) == 0 ) {
             // Users wants to make sure we have a console.
             // Try to allocate one.
             BOOL rc = ::AllocConsole();
             if ( rc ) {
@@ -403,18 +387,35 @@ nsNativeAppSupportWin::CheckConsole() {
             // Remove the console argument from the command line.
             do {
                 gArgv[i] = gArgv[i + 1];
                 ++i;
             } while (gArgv[i]);
 
             --gArgc;
 
-            // Don't bother doing this more than once.
-            break;
+        } else if ( strcmp( "-attach-console", gArgv[i] ) == 0
+                    ||
+                    strcmp( "/attach-console", gArgv[i] ) == 0 ) {
+            // Try to attach console to the parent process.
+            // It will succeed when the parent process is a command line,
+            // so that stdio will be displayed in it.
+            if (AttachConsole(ATTACH_PARENT_PROCESS)) {
+                // Change std handles to refer to new console handles.
+                // Before doing so, ensure that stdout/stderr haven't been
+                // redirected to a valid file
+                if (_fileno(stdout) == -1 ||
+                    _get_osfhandle(fileno(stdout)) == -1)
+                    freopen("CONOUT$", "w", stdout);
+                if (_fileno(stderr) == -1 ||
+                    _get_osfhandle(fileno(stderr)) == -1)
+                    freopen("CONERR$", "w", stderr);
+                if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1)
+                    freopen("CONIN$", "r", stdin);
+            }
         }
     }
 
     return;
 }
 
 
 // Create and return an instance of class nsNativeAppSupportWin.