~ubuntu-branches/ubuntu/oneiric/enigmail/oneiric-updates

« back to all changes in this revision

Viewing changes to build/wince/shunt/environment.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2010-04-10 01:42:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100410014224-fbq9ui5x3b0h2t36
Tags: 2:1.0.1-0ubuntu1
* First releaase of enigmail 1.0.1 for tbird/icedove 3
  (LP: #527138)
* redo packaging from scratch 
  + add debian/make-orig target that uses xulrunner provided
    buildsystem + enigmail tarball to produce a proper orig.tar.gz
  + use debhelper 7 with mozilla-devscripts
  + use debian source format 3.0 (quilt)
  + patch enigmail to use frozen API only
    - add debian/patches/frozen_api.diff
  + patch build system to not link against -lxul - which isnt
    available for sdks produced by all-static apps like tbird
    - add debian/patches/build_system_dont_link_libxul.diff
  + add minimal build-depends to control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is MOZCE Lib.
 
16
 *
 
17
 * The Initial Developer of the Original Code is Doug Turner <dougt@meer.net>.
 
18
 
 
19
 * Portions created by the Initial Developer are Copyright (C) 2005
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *  John Wolfe <wolfe@lobo.us>
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
/* environment.h and environment.cpp are also included by app update */
 
40
 
 
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"
 
45
#endif
 
46
#include "include/environment.h"
 
47
#include "time_conversions.h"
 
48
#include <stdlib.h>
 
49
#include "Windows.h"
 
50
 
 
51
////////////////////////////////////////////////////////
 
52
//  Environment Variable Stuff
 
53
////////////////////////////////////////////////////////
 
54
 
 
55
typedef struct env_entry env_entry;
 
56
 
 
57
#define ENV_IS_STATIC 0
 
58
#define ENV_FREE_KEY 1
 
59
#define ENV_FREE_BOTH 2
 
60
 
 
61
struct env_entry {
 
62
  char *key;
 
63
  char *value;
 
64
  int flag;
 
65
  env_entry *next;
 
66
};
 
67
 
 
68
static bool env_ready = false;
 
69
static env_entry *env_head = NULL;
 
70
 
 
71
static env_entry *
 
72
env_find_key(const char *key)
 
73
{
 
74
  env_entry *entry = env_head;
 
75
  while (entry) {
 
76
    if (strcmp(entry->key, key) == 0)
 
77
      return entry;
 
78
 
 
79
    entry = entry->next;
 
80
  }
 
81
 
 
82
  return NULL;
 
83
}
 
84
 
 
85
static void
 
86
putenv_internal(char *key, char *value, int flag)
 
87
{
 
88
  env_entry *entry = env_find_key(key);
 
89
  if (entry) {
 
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)
 
93
      free (entry->value);
 
94
    if (entry->flag == ENV_FREE_KEY)
 
95
      free (entry->key);
 
96
  } else {
 
97
    entry = new env_entry;
 
98
    entry->next = env_head;
 
99
    env_head = entry;
 
100
  }
 
101
 
 
102
  entry->key = key;
 
103
  entry->value = value;
 
104
  entry->flag = flag;
 
105
}
 
106
 
 
107
static void
 
108
init_initial_env() {
 
109
  env_ready = true;
 
110
 
 
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);
 
116
}
 
117
 
 
118
void
 
119
putenv_copy(const char *k, const char *v)
 
120
{
 
121
  if (!env_ready)
 
122
    init_initial_env();
 
123
 
 
124
  putenv_internal(strdup(k), strdup(v), ENV_FREE_BOTH);
 
125
}
 
126
 
 
127
int
 
128
putenv(const char *envstr)
 
129
{
 
130
  if (!env_ready)
 
131
    init_initial_env();
 
132
 
 
133
  char *key = strdup(envstr);
 
134
  char *value = strchr(key, '=');
 
135
  if (!value) {
 
136
    free(key);
 
137
    return -1;
 
138
  }
 
139
 
 
140
  *value++ = '\0';
 
141
 
 
142
  putenv_internal(key, value, ENV_FREE_KEY);
 
143
 
 
144
  return 0;
 
145
}
 
146
 
 
147
char *
 
148
getenv(const char* name)
 
149
{
 
150
  if (!env_ready)
 
151
    init_initial_env();
 
152
 
 
153
  env_entry *entry = env_find_key(name);
 
154
  if (entry && entry->value[0] != 0) {
 
155
    return entry->value;
 
156
  }
 
157
 
 
158
  return NULL;
 
159
}
 
160
 
 
161
char
 
162
GetEnvironmentVariableW(const unsigned short* lpName,
 
163
                        unsigned short* lpBuffer,
 
164
                        unsigned long nSize)
 
165
{
 
166
  char key[256];
 
167
  int rv = WideCharToMultiByte(CP_ACP, 0, lpName, -1, key, 255, NULL, NULL);
 
168
  if (rv <= 0)
 
169
    return 0;
 
170
 
 
171
  key[rv] = 0;
 
172
  
 
173
  char* val = getenv(key);
 
174
  
 
175
  if (!val)
 
176
    return 0;
 
177
 
 
178
  // strlen(val)+1, let MBTWC convert the nul byte for us
 
179
  return MultiByteToWideChar(CP_ACP, 0, val, strlen(val)+1, lpBuffer, nSize);
 
180
}
 
181
 
 
182
char
 
183
SetEnvironmentVariableW(const unsigned short* name,
 
184
                        const unsigned short* value)
 
185
{
 
186
  char key[256];
 
187
  char val[256];
 
188
  int rv;
 
189
 
 
190
  rv = WideCharToMultiByte(CP_ACP, 0, name, -1, key, 255, NULL, NULL);
 
191
  if (rv < 0)
 
192
    return rv;
 
193
 
 
194
  key[rv] = 0;
 
195
  
 
196
  rv = WideCharToMultiByte(CP_ACP, 0, value, -1, val, 255, NULL, NULL);
 
197
  if (rv < 0)
 
198
    return rv;
 
199
 
 
200
  val[rv] = 0;
 
201
 
 
202
  putenv_copy(key, val);
 
203
  return 0;
 
204
}
 
205
 
 
206
 
 
207
unsigned int ExpandEnvironmentStringsW(const unsigned short* lpSrc,
 
208
                                       unsigned short* lpDst,
 
209
                                       unsigned int nSize)
 
210
{
 
211
  if ( NULL == lpDst )
 
212
    return 0;
 
213
 
 
214
  unsigned int size = 0;
 
215
  unsigned int index = 0;
 
216
  unsigned int origLen = wcslen(lpSrc);
 
217
 
 
218
  const unsigned short *pIn = lpSrc;
 
219
  unsigned short *pOut = lpDst;
 
220
 
 
221
  while ( index < origLen ) {
 
222
 
 
223
    if (*pIn != L'%') {  // Regular char, copy over
 
224
      if ( size++ < nSize ) *pOut = *pIn, pOut++;
 
225
      index++, pIn++;
 
226
      continue;
 
227
    }
 
228
 
 
229
    // Have a starting '%' - look for matching '%'
 
230
    int envlen = 0;
 
231
    const unsigned short *pTmp = pIn + 1;
 
232
    while ( *pTmp != L'%' && *pTmp != L' ' ) {
 
233
      envlen++, pTmp++;
 
234
      if ( origLen < index + envlen ) {    // Ran past end of original
 
235
        while ( envlen-- ) {
 
236
          if ( size++ < nSize ) *pOut = *pIn, pOut++;
 
237
          index++, pIn++;
 
238
        }
 
239
        break;
 
240
      }
 
241
    }
 
242
 
 
243
    if ( *pTmp == L' ' ) { // Need to append through space
 
244
      while ( envlen-- ) {
 
245
        if ( size++ < nSize ) *pOut = *pIn, pOut++;
 
246
        index++, pIn++;
 
247
      }
 
248
      continue;
 
249
    }
 
250
 
 
251
    pIn++; // Move past original %
 
252
    if ( 0 == envlen ) {  // Encountered a "%%" - mapping to "%"
 
253
      if ( size++ < nSize ) *pOut = *pIn, pOut++;
 
254
      index += 2, pIn++;
 
255
    } else {
 
256
      // Encountered a "%something%" - mapping "something"
 
257
      char key[256];
 
258
      int k = WideCharToMultiByte(CP_ACP, 0, pIn, envlen, key, 255, NULL, NULL);
 
259
      key[k] = 0;
 
260
      char *pC = getenv(key);
 
261
      if ( NULL != pC ) {
 
262
        int n = MultiByteToWideChar( CP_ACP, 0, pC, -1, pOut, nSize - size );
 
263
        if ( n > 0 ) {
 
264
          size += n - 1;  // Account for trailing zero
 
265
          pOut += n - 1;
 
266
        }
 
267
      }
 
268
      index += envlen + 2;
 
269
      pIn = ++pTmp;
 
270
    }
 
271
  }
 
272
 
 
273
  if ( size < nSize ) lpDst[size] = 0;
 
274
  return size;
 
275
}
 
276
 
 
277
unsigned short *
 
278
mozce_GetEnvironmentCL()
 
279
{
 
280
  env_entry *entry = env_head;
 
281
  int len = 0;
 
282
  while (entry) {
 
283
    if (entry->flag == ENV_IS_STATIC) {
 
284
      entry = entry->next;
 
285
      continue;
 
286
    }
 
287
 
 
288
    len += strlen(entry->key);
 
289
    len += strlen(entry->value);
 
290
 
 
291
    // for each env var, 11 chars of " --environ:", 3 chars of '"="', and a null at the end
 
292
    len += 11 + 3 + 1;
 
293
 
 
294
    entry = entry->next;
 
295
  }
 
296
 
 
297
  if (len == 0) {
 
298
    return wcsdup(L"");
 
299
  }
 
300
 
 
301
  wchar_t *env = (wchar_t*) malloc(sizeof(wchar_t) * (len+1));
 
302
  if (!env)
 
303
    return NULL;
 
304
 
 
305
  entry = env_head;
 
306
 
 
307
  int pos = 0;
 
308
  while (entry) {
 
309
    if (entry->flag == ENV_IS_STATIC) {
 
310
      entry = entry->next;
 
311
      continue;
 
312
    }
 
313
 
 
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));
 
318
      entry = entry->next;
 
319
      continue;
 
320
    }
 
321
 
 
322
    wcscpy (env+pos, L" --environ:\"");
 
323
    pos += 12;
 
324
    pos += MultiByteToWideChar(CP_ACP, 0, entry->key, strlen(entry->key), env+pos, len-pos);
 
325
    env[pos++] = '=';
 
326
    pos += MultiByteToWideChar(CP_ACP, 0, entry->value, strlen(entry->value), env+pos, len-pos);
 
327
    env[pos++] = '\"';
 
328
 
 
329
    entry = entry->next;
 
330
  } 
 
331
 
 
332
  env[pos] = '\0';
 
333
 
 
334
  return env;
 
335
  
 
336
}