From: Iain Patterson Date: Sat, 1 Mar 2014 11:42:21 +0000 (+0000) Subject: Renamed un/format_environment(). X-Git-Tag: v2.22~9 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=5380a4a53d9b34d6c56ef3e8a022fcdc184593a3;p=nssm.git 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. --- diff --git a/env.cpp b/env.cpp index 5b5f34a..76c187e 100644 --- a/env.cpp +++ b/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. diff --git a/env.h b/env.h index 4e0191c..24d8987 100644 --- a/env.h +++ b/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 diff --git a/gui.cpp b/gui.cpp index 4ab19d7..ce4d6bf 100644 --- a/gui.cpp +++ b/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); diff --git a/registry.cpp b/registry.cpp index 220e060..1e801ee 100644 --- a/registry.cpp +++ b/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); diff --git a/registry.h b/registry.h index 34d75f3..6d8e33d 100644 --- a/registry.h +++ b/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 *); diff --git a/settings.cpp b/settings.cpp index 172eeba..43ae559 100644 --- a/settings.cpp +++ b/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) {