Added format_environment() and unformat_environment().

Added new functions to convert an environment block, with each KEY=value
pair separated by NULL NULL, to and from a string, with the pairs
separated by CRLF.
master
Iain Patterson 11 years ago
parent 73dbba7a2e
commit 750d1a3c6f

@ -146,27 +146,15 @@ int nssm_gui(int resource, nssm_service_t *service) {
} }
if (envlen) { if (envlen) {
/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */ TCHAR *formatted;
unsigned long i, j; unsigned long newlen;
unsigned long newlen = envlen; if (format_environment(env, envlen, &formatted, &newlen)) {
for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) newlen++; popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));
}
TCHAR *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR)); else {
if (formatted) {
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++;
}
SetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, formatted); SetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, formatted);
HeapFree(GetProcessHeap(), 0, formatted); HeapFree(GetProcessHeap(), 0, formatted);
} }
else popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));
} }
if (service->envlen && service->env_extralen) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ENVIRONMENT); if (service->envlen && service->env_extralen) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ENVIRONMENT);
} }
@ -483,28 +471,15 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
return 5; return 5;
} }
/* Strip CR and replace LF with NULL. */ TCHAR *newenv;
unsigned long newlen = 0; unsigned long newlen;
unsigned long i, j; if (unformat_environment(env, envlen, &newenv, &newlen)) {
for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++;
/* Must end with two NULLs. */
newlen += 2;
TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));
if (! newenv) {
HeapFree(GetProcessHeap(), 0, env); HeapFree(GetProcessHeap(), 0, env);
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()")); popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
cleanup_nssm_service(service); cleanup_nssm_service(service);
return 5; return 5;
} }
for (i = 0, j = 0; i < envlen; i++) {
if (env[i] == _T('\r')) continue;
if (env[i] == _T('\n')) newenv[j] = _T('\0');
else newenv[j] = env[i];
j++;
}
HeapFree(GetProcessHeap(), 0, env); HeapFree(GetProcessHeap(), 0, env);
env = newenv; env = newenv;
envlen = newlen; envlen = newlen;

@ -204,6 +204,65 @@ int set_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, un
return 0; return 0;
} }
/* 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;
}
int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) { int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) {
TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen); TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);
if (! buffer) { if (! buffer) {

@ -31,6 +31,8 @@ int create_messages();
int create_parameters(nssm_service_t *, bool); int create_parameters(nssm_service_t *, bool);
int create_exit_action(TCHAR *, const TCHAR *, bool); int create_exit_action(TCHAR *, const TCHAR *, bool);
int set_environment(TCHAR *, HKEY, TCHAR *, TCHAR **, unsigned long *); int set_environment(TCHAR *, HKEY, TCHAR *, TCHAR **, unsigned long *);
int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool); int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool);
int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool); int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool);
int set_expand_string(HKEY, TCHAR *, TCHAR *); int set_expand_string(HKEY, TCHAR *, TCHAR *);

Loading…
Cancel
Save