1*0a1e2f0eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*0a1e2f0eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*0a1e2f0eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*0a1e2f0eSAndrew Rist  * distributed with this work for additional information
6*0a1e2f0eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*0a1e2f0eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*0a1e2f0eSAndrew Rist  * "License"); you may not use this file except in compliance
9*0a1e2f0eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*0a1e2f0eSAndrew Rist  *
11*0a1e2f0eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*0a1e2f0eSAndrew Rist  *
13*0a1e2f0eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*0a1e2f0eSAndrew Rist  * software distributed under the License is distributed on an
15*0a1e2f0eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*0a1e2f0eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*0a1e2f0eSAndrew Rist  * specific language governing permissions and limitations
18*0a1e2f0eSAndrew Rist  * under the License.
19*0a1e2f0eSAndrew Rist  *
20*0a1e2f0eSAndrew Rist  *************************************************************/
21*0a1e2f0eSAndrew Rist 
22*0a1e2f0eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
25cdf0e10cSrcweir #define INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <cstddef>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tchar.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
34cdf0e10cSrcweir #define MY_STRING(s) (s), MY_LENGTH(s)
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace desktop_win32 {
37cdf0e10cSrcweir 
commandLineAppend(WCHAR * buffer,WCHAR const * text,std::size_t length)38cdf0e10cSrcweir inline WCHAR * commandLineAppend(
39cdf0e10cSrcweir     WCHAR * buffer, WCHAR const * text, std::size_t length)
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     wcsncpy(buffer, text, length + 1); // trailing null
42cdf0e10cSrcweir     return buffer + length;
43cdf0e10cSrcweir }
44cdf0e10cSrcweir 
commandLineAppend(WCHAR * buffer,WCHAR const * text)45cdf0e10cSrcweir inline WCHAR * commandLineAppend(WCHAR * buffer, WCHAR const * text) {
46cdf0e10cSrcweir     return commandLineAppend(buffer, text, wcslen(text));
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
commandLineAppendEncoded(WCHAR * buffer,WCHAR const * text)49cdf0e10cSrcweir inline WCHAR * commandLineAppendEncoded(WCHAR * buffer, WCHAR const * text) {
50cdf0e10cSrcweir     std::size_t n = 0;
51cdf0e10cSrcweir     for (;;) {
52cdf0e10cSrcweir         WCHAR c = *text++;
53cdf0e10cSrcweir         if (c == L'\0') {
54cdf0e10cSrcweir             break;
55cdf0e10cSrcweir         } else if (c == L'$') {
56cdf0e10cSrcweir             buffer = commandLineAppend(buffer, MY_STRING(L"\\$"));
57cdf0e10cSrcweir             n = 0;
58cdf0e10cSrcweir         } else if (c == L'\\') {
59cdf0e10cSrcweir             buffer = commandLineAppend(buffer, MY_STRING(L"\\\\"));
60cdf0e10cSrcweir             n += 2;
61cdf0e10cSrcweir         } else {
62cdf0e10cSrcweir             *buffer++ = c;
63cdf0e10cSrcweir             n = 0;
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir     // The command line will continue with a double quote, so double any
67cdf0e10cSrcweir     // preceding backslashes as required by Windows:
68cdf0e10cSrcweir     for (std::size_t i = 0; i < n; ++i) {
69cdf0e10cSrcweir         *buffer++ = L'\\';
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir     *buffer = L'\0';
72cdf0e10cSrcweir     return buffer;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // Set the PATH environment variable in the current (loader) process, so that a
76cdf0e10cSrcweir // following CreateProcess has the necessary environment:
77cdf0e10cSrcweir //
78cdf0e10cSrcweir // @param binPath
79cdf0e10cSrcweir // Must point to an array of size at least MAX_PATH.  Is filled with the null
80cdf0e10cSrcweir // terminated full path to the "bin" file corresponding to the current
81cdf0e10cSrcweir // executable.
82cdf0e10cSrcweir //
83cdf0e10cSrcweir // @param iniDirectory
84cdf0e10cSrcweir // Must point to an array of size at least MAX_PATH.  Is filled with the null
85cdf0e10cSrcweir // terminated full directory path (ending in "\") to the "ini" file
86cdf0e10cSrcweir // corresponding to the current executable.
87cdf0e10cSrcweir void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir #endif
92