1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER >= 1400)
28*cdf0e10cSrcweir #pragma warning(disable:4740)
29*cdf0e10cSrcweir #endif
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "macros.h"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifdef __cplusplus
34*cdf0e10cSrcweir #define local inline
35*cdf0e10cSrcweir #else
36*cdf0e10cSrcweir #define local static
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir local LPCWSTR SkipBlanks( LPCWSTR lpScan )
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 	while ( ' ' == *lpScan || '\t' == *lpScan )
42*cdf0e10cSrcweir 		lpScan++;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	return lpScan;
45*cdf0e10cSrcweir }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir local LPCWSTR SkipArgument( LPCWSTR lpScan )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	BOOL	fQuoted = FALSE;
51*cdf0e10cSrcweir 	LPCWSTR	lpArgEnd = NULL;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	do
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 		switch ( *lpScan )
56*cdf0e10cSrcweir 		{
57*cdf0e10cSrcweir 		case ' ':
58*cdf0e10cSrcweir 		case '\t':
59*cdf0e10cSrcweir 			if ( fQuoted )
60*cdf0e10cSrcweir 				lpScan++;
61*cdf0e10cSrcweir 			else
62*cdf0e10cSrcweir 				lpArgEnd = lpScan;
63*cdf0e10cSrcweir 			break;
64*cdf0e10cSrcweir 		case '\"':
65*cdf0e10cSrcweir 			lpScan++;
66*cdf0e10cSrcweir 			fQuoted = !fQuoted;
67*cdf0e10cSrcweir 			break;
68*cdf0e10cSrcweir 		case '\0':
69*cdf0e10cSrcweir 			lpArgEnd = lpScan;
70*cdf0e10cSrcweir 			break;
71*cdf0e10cSrcweir 		default:
72*cdf0e10cSrcweir 			lpScan++;
73*cdf0e10cSrcweir 			break;
74*cdf0e10cSrcweir 		}
75*cdf0e10cSrcweir 	} while( *lpScan && !lpArgEnd );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	return lpScan;
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir IMPLEMENT_THUNK( shell32, WINDOWS, LPWSTR *, WINAPI, CommandLineToArgvW, ( LPCWSTR lpCmdLineW, int *pNumArgs ) )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	LPWSTR	*lpArgvW = NULL;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	if ( !lpCmdLineW || !*lpCmdLineW )
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		CHAR	szFileName[MAX_PATH];
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 		DWORD	dwResult = GetModuleFileNameA( NULL, szFileName, MAX_PATH );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		if ( dwResult && dwResult < MAX_PATH )
92*cdf0e10cSrcweir 		{
93*cdf0e10cSrcweir 			int cchNeeded = MultiByteToWideChar( CP_ACP, 0, szFileName, -1, NULL, 0 );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 			lpArgvW = (LPWSTR *)GlobalAlloc( 0, cchNeeded * sizeof(WCHAR) + sizeof(LPWSTR) );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 			if ( lpArgvW )
98*cdf0e10cSrcweir 			{
99*cdf0e10cSrcweir 				lpArgvW[0] = (LPWSTR)(lpArgvW + 1);
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 				MultiByteToWideChar( CP_ACP, 0, szFileName, -1, lpArgvW[0], cchNeeded );
102*cdf0e10cSrcweir 				*pNumArgs = 1;
103*cdf0e10cSrcweir 			}
104*cdf0e10cSrcweir 			else
105*cdf0e10cSrcweir 				SetLastError( ERROR_OUTOFMEMORY );
106*cdf0e10cSrcweir 		}
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 	else
109*cdf0e10cSrcweir 	{
110*cdf0e10cSrcweir 		LPCWSTR	lpScan = lpCmdLineW;
111*cdf0e10cSrcweir 		int		nTokens = 0;
112*cdf0e10cSrcweir 		int		cchNeeded = 0;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 		// Count arguments and required size
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		while ( *lpScan )
117*cdf0e10cSrcweir 		{
118*cdf0e10cSrcweir 			lpScan = SkipBlanks( lpScan );
119*cdf0e10cSrcweir 			if ( *lpScan )
120*cdf0e10cSrcweir 			{
121*cdf0e10cSrcweir 				LPCWSTR	lpArgEnd = SkipArgument( lpScan );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 				nTokens++;
124*cdf0e10cSrcweir 				cchNeeded += lpArgEnd - lpScan + 1;
125*cdf0e10cSrcweir 				lpScan = lpArgEnd;
126*cdf0e10cSrcweir 			}
127*cdf0e10cSrcweir 		}
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 		// Allocate space for one additional NULL pointer to terminate list
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		lpArgvW = (LPWSTR *)GlobalAlloc( 0, sizeof(LPWSTR) * (nTokens + 1) + sizeof(WCHAR) * cchNeeded );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		if ( lpArgvW )
134*cdf0e10cSrcweir 		{
135*cdf0e10cSrcweir 			// Collect arguments
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 			LPWSTR	lpDestination = (LPWSTR)&lpArgvW[nTokens + 1];
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 			lpScan = lpCmdLineW;
140*cdf0e10cSrcweir 			nTokens = 0;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 			while ( *lpScan )
143*cdf0e10cSrcweir 			{
144*cdf0e10cSrcweir 				lpScan = SkipBlanks( lpScan );
145*cdf0e10cSrcweir 				if ( *lpScan )
146*cdf0e10cSrcweir 				{
147*cdf0e10cSrcweir 					LPCWSTR	lpArgEnd = SkipArgument( lpScan );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 					lpArgvW[nTokens++] = lpDestination;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 					while ( lpScan < lpArgEnd )
152*cdf0e10cSrcweir 					{
153*cdf0e10cSrcweir 						if ( '\"' != *lpScan )
154*cdf0e10cSrcweir 							*lpDestination++ = *lpScan;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 						lpScan++;
157*cdf0e10cSrcweir 					}
158*cdf0e10cSrcweir 					*lpDestination++ = 0;
159*cdf0e10cSrcweir 				}
160*cdf0e10cSrcweir 			}
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			lpArgvW[nTokens] = NULL;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			*pNumArgs = nTokens;
165*cdf0e10cSrcweir 		}
166*cdf0e10cSrcweir 		else
167*cdf0e10cSrcweir 			SetLastError( ERROR_OUTOFMEMORY );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	}
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	return lpArgvW;
172*cdf0e10cSrcweir }