1
/* -*- Mode: C; c-basic-offset: 2; tab-width: 4; indent-tabs-mode: nil; -*- */
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 MOZCE Lib.
17
* The Initial Developer of the Original Code is Doug Turner <dougt@meer.net>.
19
* Portions created by the Initial Developer are Copyright (C) 2005
20
* the Initial Developer. All Rights Reserved.
23
* John Wolfe <wolfe@lobo.us>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
/* environment.h and environment.cpp are also included by app update */
41
// WINCE_SKIP_SHUNT_INCLUDE is used by app update to prevent including of
42
// mozce_shunt.h when it includes environment.cpp
43
#ifndef WINCE_SKIP_SHUNT_INCLUDE
44
#include "include/mozce_shunt.h"
46
#include "include/environment.h"
47
#include "time_conversions.h"
51
////////////////////////////////////////////////////////
52
// Environment Variable Stuff
53
////////////////////////////////////////////////////////
55
typedef struct env_entry env_entry;
57
#define ENV_IS_STATIC 0
58
#define ENV_FREE_KEY 1
59
#define ENV_FREE_BOTH 2
68
static bool env_ready = false;
69
static env_entry *env_head = NULL;
72
env_find_key(const char *key)
74
env_entry *entry = env_head;
76
if (strcmp(entry->key, key) == 0)
86
putenv_internal(char *key, char *value, int flag)
88
env_entry *entry = env_find_key(key);
90
// get rid of old key/value; if flag is set, then
91
// they're static strings and we don't touch them
92
if (entry->flag == ENV_FREE_BOTH)
94
if (entry->flag == ENV_FREE_KEY)
97
entry = new env_entry;
98
entry->next = env_head;
103
entry->value = value;
111
putenv_internal("NSS_DEFAULT_DB_TYPE", "sql", ENV_IS_STATIC);
112
putenv_internal("NSPR_FD_CACHE_SIZE_LOW", "10", ENV_IS_STATIC);
113
putenv_internal("NSPR_FD_CACHE_SIZE_HIGH", "30", ENV_IS_STATIC);
114
putenv_internal("XRE_PROFILE_NAME", "default", ENV_IS_STATIC);
115
putenv_internal("tmp", "/Temp", ENV_IS_STATIC);
119
putenv_copy(const char *k, const char *v)
124
putenv_internal(strdup(k), strdup(v), ENV_FREE_BOTH);
128
putenv(const char *envstr)
133
char *key = strdup(envstr);
134
char *value = strchr(key, '=');
142
putenv_internal(key, value, ENV_FREE_KEY);
148
getenv(const char* name)
153
env_entry *entry = env_find_key(name);
154
if (entry && entry->value[0] != 0) {
162
GetEnvironmentVariableW(const unsigned short* lpName,
163
unsigned short* lpBuffer,
167
int rv = WideCharToMultiByte(CP_ACP, 0, lpName, -1, key, 255, NULL, NULL);
173
char* val = getenv(key);
178
// strlen(val)+1, let MBTWC convert the nul byte for us
179
return MultiByteToWideChar(CP_ACP, 0, val, strlen(val)+1, lpBuffer, nSize);
183
SetEnvironmentVariableW(const unsigned short* name,
184
const unsigned short* value)
190
rv = WideCharToMultiByte(CP_ACP, 0, name, -1, key, 255, NULL, NULL);
196
rv = WideCharToMultiByte(CP_ACP, 0, value, -1, val, 255, NULL, NULL);
202
putenv_copy(key, val);
207
unsigned int ExpandEnvironmentStringsW(const unsigned short* lpSrc,
208
unsigned short* lpDst,
214
unsigned int size = 0;
215
unsigned int index = 0;
216
unsigned int origLen = wcslen(lpSrc);
218
const unsigned short *pIn = lpSrc;
219
unsigned short *pOut = lpDst;
221
while ( index < origLen ) {
223
if (*pIn != L'%') { // Regular char, copy over
224
if ( size++ < nSize ) *pOut = *pIn, pOut++;
229
// Have a starting '%' - look for matching '%'
231
const unsigned short *pTmp = pIn + 1;
232
while ( *pTmp != L'%' && *pTmp != L' ' ) {
234
if ( origLen < index + envlen ) { // Ran past end of original
236
if ( size++ < nSize ) *pOut = *pIn, pOut++;
243
if ( *pTmp == L' ' ) { // Need to append through space
245
if ( size++ < nSize ) *pOut = *pIn, pOut++;
251
pIn++; // Move past original %
252
if ( 0 == envlen ) { // Encountered a "%%" - mapping to "%"
253
if ( size++ < nSize ) *pOut = *pIn, pOut++;
256
// Encountered a "%something%" - mapping "something"
258
int k = WideCharToMultiByte(CP_ACP, 0, pIn, envlen, key, 255, NULL, NULL);
260
char *pC = getenv(key);
262
int n = MultiByteToWideChar( CP_ACP, 0, pC, -1, pOut, nSize - size );
264
size += n - 1; // Account for trailing zero
273
if ( size < nSize ) lpDst[size] = 0;
278
mozce_GetEnvironmentCL()
280
env_entry *entry = env_head;
283
if (entry->flag == ENV_IS_STATIC) {
288
len += strlen(entry->key);
289
len += strlen(entry->value);
291
// for each env var, 11 chars of " --environ:", 3 chars of '"="', and a null at the end
301
wchar_t *env = (wchar_t*) malloc(sizeof(wchar_t) * (len+1));
309
if (entry->flag == ENV_IS_STATIC) {
314
if (strchr(entry->key, '"') || strchr(entry->value, '"')) {
315
// argh, we don't have a good way of encoding the ", so let's just
316
// ignore this var for now
317
RETAILMSG(1, (L"Skipping environment variable with quote marks in key or value! %S -> %s\r\n", entry->key, entry->value));
322
wcscpy (env+pos, L" --environ:\"");
324
pos += MultiByteToWideChar(CP_ACP, 0, entry->key, strlen(entry->key), env+pos, len-pos);
326
pos += MultiByteToWideChar(CP_ACP, 0, entry->value, strlen(entry->value), env+pos, len-pos);