bug 465874 - WinCE Environment Variables Have To Be Passed On Command Line r=bsmedberg
authorBrad Lassey <blassey@mozilla.com>
Thu, 05 Feb 2009 15:50:44 -0800
changeset 24686 95732ec8087c91ce7dec0f2e6d4ca678231c8152
parent 24685 7f0aab2e33b6204028271c3111c28f8145594bac
child 24687 38bba72321ca069d687786b9fca02bbe000ca2ff
push id5166
push userblassey@mozilla.com
push dateThu, 05 Feb 2009 23:51:22 +0000
treeherdermozilla-central@38bba72321ca [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs465874
milestone1.9.2a1pre
bug 465874 - WinCE Environment Variables Have To Be Passed On Command Line r=bsmedberg
build/wince/shunt/include/mozce_shunt.h
build/wince/shunt/map.cpp
toolkit/xre/nsWindowsRestart.cpp
toolkit/xre/nsWindowsWMain.cpp
--- a/build/wince/shunt/include/mozce_shunt.h
+++ b/build/wince/shunt/include/mozce_shunt.h
@@ -98,13 +98,15 @@ MOZCE_SHUNT_API int _unlink(const char *
 /* The locale stuff should be defined here, but it can't be because it
    is already defined in locale.h.
   
  MOZCE_SHUNT_API struct lconv * localeconv(void)
   
 */
 
 
+MOZCE_SHUNT_API unsigned short* mozce_GetEnvironmentCL();
+
 #ifdef __cplusplus
 };
 #endif
 
 #endif //MOZCE_SHUNT_H
--- a/build/wince/shunt/map.cpp
+++ b/build/wince/shunt/map.cpp
@@ -31,19 +31,18 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-
- #include "stdlib.h"
- #include "Windows.h"
+#include "stdlib.h"
+#include "Windows.h"
  
 #include "mozce_shunt.h"
 #include "time_conversions.h"
 
 ////////////////////////////////////////////////////////
 //  Environment Variable Stuff
 ////////////////////////////////////////////////////////
 
@@ -60,24 +59,21 @@ static mapping initial_map[] = {
 #ifdef DEBUG_NSPR_ALL
     {"NSPR_LOG_MODULES", "all:5",initial_map + (init_i++)},
     {"NSPR_LOG_FILE","nspr.log",initial_map + (init_i++)},
 #endif  
 #ifdef TIMELINE
     {"NS_TIMELINE_LOG_FILE","\\bin\\timeline.log",initial_map + (init_i++)},
     {"NS_TIMELINE_ENABLE", "1",initial_map + (init_i++)},
 #endif
-    {"tmp", "/Temp",initial_map + (init_i++)},
-    {"GRE_HOME",".",initial_map + (init_i++)},
     {"NSS_DEFAULT_DB_TYPE", "sql",initial_map + (init_i++)},
     {"NSPR_FD_CACHE_SIZE_LOW", "10",initial_map + (init_i++)},              
     {"NSPR_FD_CACHE_SIZE_HIGH", "30",initial_map + (init_i++)},
-    {"XRE_PROFILE_PATH", "\\Application Data\\Mozilla\\Profiles",initial_map + (init_i++)},
-    {"XRE_PROFILE_LOCAL_PATH","./profile",initial_map + (init_i++)},
-    {"XRE_PROFILE_NAME","default",0}
+    {"XRE_PROFILE_NAME","default",initial_map + (init_i++)},
+    {"tmp", "/Temp", 0 }
 };
 
 static mapping* head = initial_map;
 
  
 mapping* getMapping(const char* key)
 {
   mapping* cur = head;
@@ -141,32 +137,31 @@ MOZCE_SHUNT_API char GetEnvironmentVaria
   int rv = WideCharToMultiByte(CP_ACP,
                                0,
                                lpName,
                                -1,
                                key,
                                256,
                                NULL,
                                NULL);
-  if(rv < 0)
-    return rv;
+  if(rv <= 0)
+    return 0;
   
   char* val = map_get(key);
   
   if(val) 
     {
-      MultiByteToWideChar(CP_ACP,
-                          0,
-                          val,
-                          strlen(val)+1,
-                          lpBuffer,
-                          nSize );
-      return ERROR_SUCCESS;
+      return MultiByteToWideChar(CP_ACP,
+                                 0,
+                                 val,
+                                 strlen(val)+1,
+                                 lpBuffer,
+                                  nSize);
     }
-  return -1;
+  return 0;
 }
 
 MOZCE_SHUNT_API char SetEnvironmentVariableW( const unsigned short * name, const unsigned short * value )
 {
   char key[256];
   char val[256];
   int rv = WideCharToMultiByte(CP_ACP,
                                0,
@@ -579,18 +574,47 @@ static struct lconv s_locale_conv =
     1,     /* p_sign_posn */
     1,     /* n_sign_posn */
   };
 
 MOZCE_SHUNT_API struct lconv * localeconv(void)
 {
   return &s_locale_conv;
 }
- 
+
+MOZCE_SHUNT_API
+unsigned short* mozce_GetEnvironmentCL()
+{
+  mapping* cur = head;
+  int len = 0;
+  while(cur != NULL){
+    len+=strlen(cur->key);
+	len+=strlen(cur->value);
+	len+=15;
+    cur = cur->next;
+  }	
 
+  unsigned short* env = (unsigned short*)malloc(sizeof(wchar_t)*(len +1));
+  cur = head;
+  int pos = 0;
+  while(cur != NULL){
+	wcscpy(env+pos, L" --environ:\"");
+	pos+=12;
+    pos+=MultiByteToWideChar( CP_ACP, 0, cur->key, -1, env+pos, len-pos );
+	env[pos-1] = '=';
+	pos+=MultiByteToWideChar( CP_ACP, 0, cur->value, -1, env+pos, len-pos );
+	env[pos-1]='\"';
+
+    cur = cur->next;
+  }	
+
+  env[pos] = '\0';
+  return env;
+  
+}
 
 BOOL WINAPI DllMain(HANDLE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
 {
   // Perform actions based on the reason for calling.
   switch( fdwReason ) 
   { 
     case DLL_PROCESS_ATTACH:
       // Initialize once for each new process.
--- a/toolkit/xre/nsWindowsRestart.cpp
+++ b/toolkit/xre/nsWindowsRestart.cpp
@@ -167,16 +167,21 @@ MakeCommandLine(int argc, PRUnichar **ar
 {
   int i;
   int len = 0;
 
   // The + 1 of the last argument handles the allocation for null termination
   for (i = 0; i < argc; ++i)
     len += ArgStrLen(argv[i]) + 1;
 
+#ifdef WINCE
+  wchar_t *env = mozce_GetEnvironmentCL();
+  len += (wcslen(env));
+#endif
+
   // Protect against callers that pass 0 arguments
   if (len == 0)
     len = 1;
 
   PRUnichar *s = (PRUnichar*) malloc(len * sizeof(PRUnichar));
   if (!s)
     return NULL;
 
@@ -186,16 +191,19 @@ MakeCommandLine(int argc, PRUnichar **ar
     if (i + 1 != argc) {
       *c = ' ';
       ++c;
     }
   }
 
   *c = '\0';
 
+#ifdef WINCE
+  wcscat(s, env);
+#endif
   return s;
 }
 
 /**
  * Convert UTF8 to UTF16 without using the normal XPCOM goop, which we
  * can't link to updater.exe.
  */
 static PRUnichar*
@@ -252,16 +260,29 @@ WinLaunchChild(const PRUnichar *exePath,
   return ok;
 }
 
 BOOL
 WinLaunchChild(const PRUnichar *exePath, int argc, PRUnichar **argv)
 {
   PRUnichar *cl;
   BOOL ok;
+
+#ifdef WINCE
+  // Windows Mobile Issue: 
+  // When passing both an image name and a command line to
+  // CreateProcessW, you need to make sure that the image name
+  // identially matches the first argument of the command line.  If
+  // they do not match, Windows Mobile will send two "argv[0]" values.
+  // To avoid this problem, we will strip off the argv here, and
+  // depend only on the exePath.
+  argv = argv + 1;
+  argc--;
+#endif
+
   cl = MakeCommandLine(argc, argv);
   if (!cl)
     return FALSE;
 
   STARTUPINFOW si = {sizeof(si), 0};
   PROCESS_INFORMATION pi = {0};
 
   ok = CreateProcessW(exePath,
@@ -273,14 +294,27 @@ WinLaunchChild(const PRUnichar *exePath,
                       NULL,  // inherit my environment
                       NULL,  // use my current directory
                       &si,
                       &pi);
 
   if (ok) {
     CloseHandle(pi.hProcess);
     CloseHandle(pi.hThread);
+  } else {
+    LPVOID lpMsgBuf;
+    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+		  FORMAT_MESSAGE_FROM_SYSTEM |
+		  FORMAT_MESSAGE_IGNORE_INSERTS,
+		  NULL,
+		  GetLastError(),
+		  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+		  (LPTSTR) &lpMsgBuf,
+		  0,
+		  NULL
+		  );
+    wprintf(L"Error restarting: %s\n", lpMsgBuf);
   }
 
   free(cl);
 
   return ok;
 }
--- a/toolkit/xre/nsWindowsWMain.cpp
+++ b/toolkit/xre/nsWindowsWMain.cpp
@@ -57,18 +57,38 @@ FreeAllocStrings(int argc, char **argv)
   while (argc) {
     --argc;
     delete [] argv[argc];
   }
 
   delete [] argv;
 }
 
+#ifdef WINCE
+/** argc/argv are in/out parameters */
+void ExtractEnvironmentFromCL(int &argc, WCHAR **&argv)
+{
+  for (int x = 0; x < argc; ++x) {
+    if (!wcsncmp(argv[x], L"--environ:",10)) {
+      char* key_val = AllocConvertUTF16toUTF8(argv[x]+10);
+      putenv(key_val);
+      free(key_val);
+      argc -= 1;
+      memcpy(&argv[x], &argv[x+1], (argc - x) * sizeof(WCHAR*));      
+    }
+  } 
+}
+#endif  
+
+
 int wmain(int argc, WCHAR **argv)
 {
+#ifdef WINCE
+  ExtractEnvironmentFromCL(argc, argv);
+#endif
   char **argvConverted = new char*[argc + 1];
   if (!argvConverted)
     return 127;
 
   for (int i = 0; i < argc; ++i) {
     argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
     if (!argvConverted[i]) {
       return 127;