1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Simon Fraser <sfraser@netscape.com>
24 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Mark Mentovai <mark@moxienet.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsCommandLineServiceMac.h"
43 #include <CoreFoundation/CoreFoundation.h>
44 #include <Carbon/Carbon.h>
46 namespace CommandLineServiceMac
{
48 static const int kArgsGrowSize
= 20;
50 static char** sArgs
= NULL
;
51 static int sArgsAllocated
= 0;
52 static int sArgsUsed
= 0;
54 void AddToCommandLine(const char* inArgText
)
56 if (sArgsUsed
>= sArgsAllocated
- 1) {
57 // realloc does not free the given pointer if allocation fails
58 char **temp
= static_cast<char**>(realloc(sArgs
, (sArgsAllocated
+ kArgsGrowSize
) * sizeof(char*)));
62 sArgsAllocated
+= kArgsGrowSize
;
65 char *temp2
= strdup(inArgText
);
69 sArgs
[sArgsUsed
++] = temp2
;
70 sArgs
[sArgsUsed
] = NULL
;
75 void SetupMacCommandLine(int& argc
, char**& argv
, PRBool forRestart
)
77 sArgs
= static_cast<char **>(malloc(kArgsGrowSize
* sizeof(char*)));
80 sArgsAllocated
= kArgsGrowSize
;
84 // Copy args, stripping anything we don't want.
85 for (int arg
= 0; arg
< argc
; arg
++) {
86 char* flag
= argv
[arg
];
87 // Don't pass on the psn (Process Serial Number) flag from the OS.
88 if (strncmp(flag
, "-psn_", 5) != 0)
89 AddToCommandLine(flag
);
93 // If the process will be relaunched, the child should be in the foreground
94 // if the parent is in the foreground. This will be communicated in a
95 // command-line argument to the child.
96 Boolean isForeground
= false;
97 ProcessSerialNumber psnSelf
, psnFront
;
98 if (::GetCurrentProcess(&psnSelf
) == noErr
&&
99 ::GetFrontProcess(&psnFront
) == noErr
&&
100 ::SameProcess(&psnSelf
, &psnFront
, &isForeground
) == noErr
&&
102 AddToCommandLine("-foreground");
110 } // namespace CommandLineServiceMac