Browse Source

Renamed un/format_environment().

The format_environment() and unformat_environment() functions could be
used to format any double null-terminated string list, and are not
specificially tied to formatting environment blocks.
Iain Patterson 7 years ago
parent
commit
5380a4a53d
6 changed files with 65 additions and 65 deletions
  1. 0 59
      env.cpp
  2. 0 2
      env.h
  3. 2 2
      gui.cpp
  4. 59 0
      registry.cpp
  5. 2 0
      registry.h
  6. 2 2
      settings.cpp

+ 0 - 59
env.cpp

@@ -104,65 +104,6 @@ int duplicate_environment(TCHAR *rawenv) {
   return ret;
 }
 
-/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
-int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {
-  unsigned long i, j;
-  *newlen = envlen;
-
-  if (! *newlen) {
-    *formatted = 0;
-    return 0;
-  }
-
-  for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;
-
-  *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
-  if (! *formatted) {
-    *newlen = 0;
-    return 1;
-  }
-
-  for (i = 0, j = 0; i < envlen; i++) {
-    (*formatted)[j] = env[i];
-    if (! env[i]) {
-      if (env[i + 1]) {
-        (*formatted)[j] = _T('\r');
-        (*formatted)[++j] = _T('\n');
-      }
-    }
-    j++;
-  }
-
-  return 0;
-}
-
-/* Strip CR and replace LF with NULL. */
-int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {
-  unsigned long i, j;
-  *newlen = 0;
-
-  if (! envlen) {
-    *unformatted = 0;
-    return 0;
-  }
-
-  for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;
-  /* Must end with two NULLs. */
-  *newlen += 2;
-
-  *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
-  if (! *unformatted) return 1;
-
-  for (i = 0, j = 0; i < envlen; i++) {
-    if (env[i] == _T('\r')) continue;
-    if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');
-    else (*unformatted)[j] = env[i];
-    j++;
-  }
-
-  return 0;
-}
-
 /*
   Verify an environment block.
   Returns:  1 if environment is invalid.

+ 0 - 2
env.h

@@ -6,8 +6,6 @@ TCHAR *expand_environment_string(TCHAR *);
 int set_environment_block(TCHAR *);
 int clear_environment();
 int duplicate_environment(TCHAR *);
-int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
-int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
 int test_environment(TCHAR *);
 
 #endif

+ 2 - 2
gui.cpp

@@ -184,7 +184,7 @@ int nssm_gui(int resource, nssm_service_t *service) {
     if (envlen) {
       TCHAR *formatted;
       unsigned long newlen;
-      if (format_environment(env, envlen, &formatted, &newlen)) {
+      if (format_double_null(env, envlen, &formatted, &newlen)) {
         popup_message(dlg, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));
       }
       else {
@@ -551,7 +551,7 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
 
     TCHAR *newenv;
     unsigned long newlen;
-    if (unformat_environment(env, envlen, &newenv, &newlen)) {
+    if (unformat_double_null(env, envlen, &newenv, &newlen)) {
       HeapFree(GetProcessHeap(), 0, env);
       popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
       cleanup_nssm_service(service);

+ 59 - 0
registry.cpp

@@ -349,6 +349,65 @@ int get_number(HKEY key, TCHAR *value, unsigned long *number) {
   return get_number(key, value, number, true);
 }
 
+/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
+int format_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **formatted, unsigned long *newlen) {
+  unsigned long i, j;
+  *newlen = dnlen;
+
+  if (! *newlen) {
+    *formatted = 0;
+    return 0;
+  }
+
+  for (i = 0; i < dnlen; i++) if (! dn[i] && dn[i + 1]) ++*newlen;
+
+  *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
+  if (! *formatted) {
+    *newlen = 0;
+    return 1;
+  }
+
+  for (i = 0, j = 0; i < dnlen; i++) {
+    (*formatted)[j] = dn[i];
+    if (! dn[i]) {
+      if (dn[i + 1]) {
+        (*formatted)[j] = _T('\r');
+        (*formatted)[++j] = _T('\n');
+      }
+    }
+    j++;
+  }
+
+  return 0;
+}
+
+/* Strip CR and replace LF with NULL. */
+int unformat_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **unformatted, unsigned long *newlen) {
+  unsigned long i, j;
+  *newlen = 0;
+
+  if (! dnlen) {
+    *unformatted = 0;
+    return 0;
+  }
+
+  for (i = 0; i < dnlen; i++) if (dn[i] != _T('\r')) ++*newlen;
+  /* Must end with two NULLs. */
+  *newlen += 2;
+
+  *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
+  if (! *unformatted) return 1;
+
+  for (i = 0, j = 0; i < dnlen; i++) {
+    if (dn[i] == _T('\r')) continue;
+    if (dn[i] == _T('\n')) (*unformatted)[j] = _T('\0');
+    else (*unformatted)[j] = dn[i];
+    j++;
+  }
+
+  return 0;
+}
+
 void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned long *buffer, unsigned long default_value, unsigned long event) {
   unsigned long type = REG_DWORD;
   unsigned long buflen = sizeof(unsigned long);

+ 2 - 0
registry.h

@@ -46,6 +46,8 @@ int set_expand_string(HKEY, TCHAR *, TCHAR *);
 int set_number(HKEY, TCHAR *, unsigned long);
 int get_number(HKEY, TCHAR *, unsigned long *, bool);
 int get_number(HKEY, TCHAR *, unsigned long *);
+int format_double_null(TCHAR *, unsigned long, TCHAR **, unsigned long *);
+int unformat_double_null(TCHAR *, unsigned long, TCHAR **, unsigned long *);
 void override_milliseconds(TCHAR *, HKEY, TCHAR *, unsigned long *, unsigned long, unsigned long);
 int get_io_parameters(nssm_service_t *, HKEY);
 int get_parameters(nssm_service_t *, STARTUPINFO *);

+ 2 - 2
settings.cpp

@@ -305,7 +305,7 @@ static int setting_set_environment(const TCHAR *service_name, void *param, const
   unsigned long envlen = (unsigned long) _tcslen(value->string) + 1;
   TCHAR *unformatted = 0;
   unsigned long newlen;
-  if (unformat_environment(value->string, envlen, &unformatted, &newlen)) return -1;
+  if (unformat_double_null(value->string, envlen, &unformatted, &newlen)) return -1;
 
   if (test_environment(unformatted)) {
     HeapFree(GetProcessHeap(), 0, unformatted);
@@ -334,7 +334,7 @@ static int setting_get_environment(const TCHAR *service_name, void *param, const
 
   TCHAR *formatted;
   unsigned long newlen;
-  if (format_environment(env, envlen, &formatted, &newlen)) return -1;
+  if (format_double_null(env, envlen, &formatted, &newlen)) return -1;
 
   int ret;
   if (additional) {