xref: /trunk/main/sal/systools/win32/uwinapi/CommandLineToArgvW.cpp (revision 24297db34623acd4ed3cd634e301c7c8a91fabbc)
1*fc0bc008SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*fc0bc008SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*fc0bc008SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*fc0bc008SAndrew Rist  * distributed with this work for additional information
6*fc0bc008SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*fc0bc008SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*fc0bc008SAndrew Rist  * "License"); you may not use this file except in compliance
9*fc0bc008SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*fc0bc008SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*fc0bc008SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*fc0bc008SAndrew Rist  * software distributed under the License is distributed on an
15*fc0bc008SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*fc0bc008SAndrew Rist  * KIND, either express or implied.  See the License for the
17*fc0bc008SAndrew Rist  * specific language governing permissions and limitations
18*fc0bc008SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*fc0bc008SAndrew Rist  *************************************************************/
21*fc0bc008SAndrew Rist 
22*fc0bc008SAndrew Rist 
23cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER >= 1400)
24cdf0e10cSrcweir #pragma warning(disable:4740)
25cdf0e10cSrcweir #endif
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "macros.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifdef __cplusplus
30cdf0e10cSrcweir #define local inline
31cdf0e10cSrcweir #else
32cdf0e10cSrcweir #define local static
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
SkipBlanks(LPCWSTR lpScan)35cdf0e10cSrcweir local LPCWSTR SkipBlanks( LPCWSTR lpScan )
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     while ( ' ' == *lpScan || '\t' == *lpScan )
38cdf0e10cSrcweir         lpScan++;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     return lpScan;
41cdf0e10cSrcweir }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
SkipArgument(LPCWSTR lpScan)44cdf0e10cSrcweir local LPCWSTR SkipArgument( LPCWSTR lpScan )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     BOOL    fQuoted = FALSE;
47cdf0e10cSrcweir     LPCWSTR lpArgEnd = NULL;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     do
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         switch ( *lpScan )
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir         case ' ':
54cdf0e10cSrcweir         case '\t':
55cdf0e10cSrcweir             if ( fQuoted )
56cdf0e10cSrcweir                 lpScan++;
57cdf0e10cSrcweir             else
58cdf0e10cSrcweir                 lpArgEnd = lpScan;
59cdf0e10cSrcweir             break;
60cdf0e10cSrcweir         case '\"':
61cdf0e10cSrcweir             lpScan++;
62cdf0e10cSrcweir             fQuoted = !fQuoted;
63cdf0e10cSrcweir             break;
64cdf0e10cSrcweir         case '\0':
65cdf0e10cSrcweir             lpArgEnd = lpScan;
66cdf0e10cSrcweir             break;
67cdf0e10cSrcweir         default:
68cdf0e10cSrcweir             lpScan++;
69cdf0e10cSrcweir             break;
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir     } while( *lpScan && !lpArgEnd );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     return lpScan;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir IMPLEMENT_THUNK( shell32, WINDOWS, LPWSTR *, WINAPI, CommandLineToArgvW, ( LPCWSTR lpCmdLineW, int *pNumArgs ) )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     LPWSTR  *lpArgvW = NULL;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     if ( !lpCmdLineW || !*lpCmdLineW )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         CHAR    szFileName[MAX_PATH];
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         DWORD   dwResult = GetModuleFileNameA( NULL, szFileName, MAX_PATH );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if ( dwResult && dwResult < MAX_PATH )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             int cchNeeded = MultiByteToWideChar( CP_ACP, 0, szFileName, -1, NULL, 0 );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             lpArgvW = (LPWSTR *)GlobalAlloc( 0, cchNeeded * sizeof(WCHAR) + sizeof(LPWSTR) );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             if ( lpArgvW )
94cdf0e10cSrcweir             {
95cdf0e10cSrcweir                 lpArgvW[0] = (LPWSTR)(lpArgvW + 1);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir                 MultiByteToWideChar( CP_ACP, 0, szFileName, -1, lpArgvW[0], cchNeeded );
98cdf0e10cSrcweir                 *pNumArgs = 1;
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir             else
101cdf0e10cSrcweir                 SetLastError( ERROR_OUTOFMEMORY );
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir     else
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         LPCWSTR lpScan = lpCmdLineW;
107cdf0e10cSrcweir         int     nTokens = 0;
108cdf0e10cSrcweir         int     cchNeeded = 0;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         // Count arguments and required size
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         while ( *lpScan )
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             lpScan = SkipBlanks( lpScan );
115cdf0e10cSrcweir             if ( *lpScan )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 LPCWSTR lpArgEnd = SkipArgument( lpScan );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir                 nTokens++;
120cdf0e10cSrcweir                 cchNeeded += lpArgEnd - lpScan + 1;
121cdf0e10cSrcweir                 lpScan = lpArgEnd;
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // Allocate space for one additional NULL pointer to terminate list
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         lpArgvW = (LPWSTR *)GlobalAlloc( 0, sizeof(LPWSTR) * (nTokens + 1) + sizeof(WCHAR) * cchNeeded );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         if ( lpArgvW )
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             // Collect arguments
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             LPWSTR  lpDestination = (LPWSTR)&lpArgvW[nTokens + 1];
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             lpScan = lpCmdLineW;
136cdf0e10cSrcweir             nTokens = 0;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             while ( *lpScan )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 lpScan = SkipBlanks( lpScan );
141cdf0e10cSrcweir                 if ( *lpScan )
142cdf0e10cSrcweir                 {
143cdf0e10cSrcweir                     LPCWSTR lpArgEnd = SkipArgument( lpScan );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir                     lpArgvW[nTokens++] = lpDestination;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                     while ( lpScan < lpArgEnd )
148cdf0e10cSrcweir                     {
149cdf0e10cSrcweir                         if ( '\"' != *lpScan )
150cdf0e10cSrcweir                             *lpDestination++ = *lpScan;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                         lpScan++;
153cdf0e10cSrcweir                     }
154cdf0e10cSrcweir                     *lpDestination++ = 0;
155cdf0e10cSrcweir                 }
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             lpArgvW[nTokens] = NULL;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             *pNumArgs = nTokens;
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         else
163cdf0e10cSrcweir             SetLastError( ERROR_OUTOFMEMORY );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     return lpArgvW;
168cdf0e10cSrcweir }
169