#include "nssm.h"\r
\r
/* See if we were launched from a console window. */\r
-void check_console() {\r
+bool check_console() {\r
/* If we're running in a service context there will be no console window. */\r
HWND console = GetConsoleWindow();\r
- if (! console) return;\r
+ if (! console) return false;\r
\r
unsigned long pid;\r
- if (! GetWindowThreadProcessId(console, &pid)) return;\r
+ if (! GetWindowThreadProcessId(console, &pid)) return false;\r
\r
/*\r
If the process associated with the console window handle is the same as\r
this process, we were not launched from an existing console. The user\r
probably double-clicked our executable.\r
*/\r
- if (GetCurrentProcessId() != pid) return;\r
+ if (GetCurrentProcessId() != pid) return true;\r
\r
/* We close our new console so that subsequent messages appear in a popup. */\r
FreeConsole();\r
+ return false;\r
}\r
\r
/* Helpers for drawing the banner. */\r
#ifndef CONSOLE_H\r
#define CONSOLE_H\r
\r
-void check_console();\r
+bool check_console();\r
void alloc_console(nssm_service_t *);\r
\r
#endif\r
static TCHAR imagepath[PATH_LENGTH];\r
static TCHAR imageargv0[PATH_LENGTH];\r
\r
+void nssm_exit(int status) {\r
+ unsetup_utf8();\r
+ exit(status);\r
+}\r
+\r
/* Are two strings case-insensitively equivalent? */\r
int str_equiv(const TCHAR *a, const TCHAR *b) {\r
size_t len = _tcslen(a);\r
}\r
\r
int _tmain(int argc, TCHAR **argv) {\r
- check_console();\r
-\r
-#ifdef UNICODE\r
- /*\r
- Ensure we write in UTF-16 mode, so that non-ASCII characters don't get\r
- mangled. If we were compiled in ANSI mode it won't work.\r
- */\r
- _setmode(_fileno(stdout), _O_U16TEXT);\r
- _setmode(_fileno(stderr), _O_U16TEXT);\r
-#endif\r
+ if (check_console()) setup_utf8();\r
\r
/* Remember if we are admin */\r
check_admin();\r
\r
/* Set up function pointers. */\r
- if (get_imports()) exit(111);\r
+ if (get_imports()) nssm_exit(111);\r
\r
/* Remember our path for later. */\r
_sntprintf_s(imageargv0, _countof(imageargv0), _TRUNCATE, _T("%s"), argv[0]);\r
Valid commands are:\r
start, stop, pause, continue, install, edit, get, set, reset, unset, remove\r
*/\r
- if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
- if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("start"))) nssm_exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("stop"))) nssm_exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));\r
if (str_equiv(argv[1], _T("restart"))) {\r
int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2);\r
- if (ret) exit(ret);\r
- exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
+ if (ret) nssm_exit(ret);\r
+ nssm_exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
}\r
- if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));\r
- if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));\r
- if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));\r
- if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("pause"))) nssm_exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("continue"))) nssm_exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("status"))) nssm_exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("rotate"))) nssm_exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2));\r
if (str_equiv(argv[1], _T("install"))) {\r
- if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL));\r
+ if (! is_admin) nssm_exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL));\r
create_messages();\r
- exit(pre_install_service(argc - 2, argv + 2));\r
+ nssm_exit(pre_install_service(argc - 2, argv + 2));\r
}\r
if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset")) || str_equiv(argv[1], _T("dump"))) {\r
int ret = pre_edit_service(argc - 1, argv + 1);\r
- if (ret == 3 && ! is_admin && argc == 3) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT));\r
+ if (ret == 3 && ! is_admin && argc == 3) nssm_exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT));\r
/* There might be a password here. */\r
for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));\r
- exit(ret);\r
+ nssm_exit(ret);\r
}\r
- if (str_equiv(argv[1], _T("list"))) exit(list_nssm_services(argc - 2, argv + 2));\r
- if (str_equiv(argv[1], _T("processes"))) exit(service_process_tree(argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("list"))) nssm_exit(list_nssm_services(argc - 2, argv + 2));\r
+ if (str_equiv(argv[1], _T("processes"))) nssm_exit(service_process_tree(argc - 2, argv + 2));\r
if (str_equiv(argv[1], _T("remove"))) {\r
- if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE));\r
- exit(pre_remove_service(argc - 2, argv + 2));\r
+ if (! is_admin) nssm_exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE));\r
+ nssm_exit(pre_remove_service(argc - 2, argv + 2));\r
}\r
}\r
\r
if (! StartServiceCtrlDispatcher(table)) {\r
unsigned long error = GetLastError();\r
/* User probably ran nssm with no argument */\r
- if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
+ if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) nssm_exit(usage(1));\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
free_imports();\r
- exit(100);\r
+ nssm_exit(100);\r
}\r
}\r
- else exit(usage(1));\r
+ else nssm_exit(usage(1));\r
\r
/* And nothing more to do */\r
- exit(0);\r
+ nssm_exit(0);\r
}\r
#include <shlwapi.h>\r
#include <stdarg.h>\r
#include <stdio.h>\r
+#include "utf8.h"\r
#include "service.h"\r
#include "account.h"\r
#include "console.h"\r
#include "gui.h"\r
#endif\r
\r
+void nssm_exit(int);\r
int str_equiv(const TCHAR *, const TCHAR *);\r
int quote(const TCHAR *, TCHAR *, size_t);\r
void strip_basename(TCHAR *);\r
RelativePath="settings.cpp"\r
>\r
</File>\r
+ <File\r
+ RelativePath="utf8.cpp"\r
+ >\r
+ </File>\r
</Filter>\r
<Filter\r
Name="Header Files"\r
RelativePath="settings.h"\r
>\r
</File>\r
+ <File\r
+ RelativePath="utf8.h"\r
+ >\r
+ </File>\r
</Filter>\r
<Filter\r
Name="Resource Files"\r
<Tool\r
Name="VCCustomBuildTool"\r
Description="Compiling messages"\r
- CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
+ CommandLine="mc -u -U $(InputName).mc -r . -h .
"\r
Outputs="$(InputName).rc;$(InputName).h"\r
/>\r
</FileConfiguration>\r
<Tool\r
Name="VCCustomBuildTool"\r
Description="Compiling messages"\r
- CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
+ CommandLine="mc -u -U $(InputName).mc -r . -h .
"\r
Outputs="$(InputName).rc;$(InputName).h"\r
/>\r
</FileConfiguration>\r
<Tool\r
Name="VCCustomBuildTool"\r
Description="Compiling messages"\r
- CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
+ CommandLine="mc -u -U $(InputName).mc -r . -h .
"\r
AdditionalDependencies=""\r
Outputs="$(InputName).rc;$(InputName).h"\r
/>\r
<Tool\r
Name="VCCustomBuildTool"\r
Description="Compiling messages"\r
- CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
+ CommandLine="mc -u -U $(InputName).mc -r . -h .
"\r
AdditionalDependencies=""\r
Outputs="$(InputName).rc;$(InputName).h"\r
/>\r
stop_service(service, exitcode, false, default_action);\r
wait_for_hooks(service, false);\r
free_imports();\r
- exit(exitcode);\r
+ nssm_exit(exitcode);\r
}\r
}\r
\r
--- /dev/null
+#include "nssm.h"
+
+static unsigned long cp;
+
+void setup_utf8() {
+#ifdef UNICODE
+ /*
+ Ensure we write in UTF-8 mode, so that non-ASCII characters don't get
+ mangled. If we were compiled in ANSI mode it won't work.
+ */
+ cp = GetConsoleOutputCP();
+ SetConsoleOutputCP(CP_UTF8);
+ _setmode(_fileno(stdout), _O_U8TEXT);
+ _setmode(_fileno(stderr), _O_U8TEXT);
+#endif
+}
+
+void unsetup_utf8() {
+ if (cp) SetConsoleOutputCP(cp);
+}
+
+/*
+ Conversion functions.
+
+ to_utf8/16() converts a string which may be either utf8 or utf16 to
+ the desired format. If no conversion is needed a new string is still
+ allocated and the old string is copied.
+
+ from_utf8/16() converts a string which IS in the specified format to
+ whichever format is needed according to whether UNICODE is defined.
+ It simply wraps the appropriate to_utf8/16() function.
+
+ Therefore the caller must ALWAYS free the destination pointer after a
+ successful (return code 0) call to one of these functions.
+
+ The length pointer is optional. Pass NULL if you don't care about
+ the length of the converted string.
+
+ Both the destination and the length, if supplied, will be zeroed if
+ no conversion was done.
+*/
+int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {
+ *utf8 = 0;
+ if (utf8len) *utf8len = 0;
+ int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
+ if (! size) return 1;
+
+ *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
+ if (! *utf8) return 2;
+
+ if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, (LPSTR) utf8, size, NULL, NULL)) {
+ HeapFree(GetProcessHeap(), 0, *utf8);
+ *utf8 = 0;
+ return 3;
+ }
+
+ if (utf8len) *utf8len = (unsigned long) strlen(*utf8);
+
+ return 0;
+}
+
+int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {
+ *utf8 = 0;
+ if (utf8len) *utf8len = 0;
+ size_t len = strlen(ansi);
+ int size = (int) len + 1;
+
+ *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
+ if (! *utf8) return 2;
+
+ if (utf8len) *utf8len = (unsigned long) len;
+ memmove(*utf8, ansi, size);
+
+ return 0;
+}
+
+int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {
+ *utf16 = 0;
+ if (utf16len) *utf16len = 0;
+ int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
+ if (! size) return 1;
+
+ *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));
+ if (! *utf16) return 2;
+
+ if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {
+ HeapFree(GetProcessHeap(), 0, *utf16);
+ *utf16 = 0;
+ return 3;
+ }
+
+ if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);
+
+ return 0;
+}
+
+int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {
+ *utf16 = 0;
+ if (utf16len) *utf16len = 0;
+ size_t len = wcslen(unicode);
+ int size = ((int) len + 1) * sizeof(wchar_t);
+
+ *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);
+ if (! *utf16) return 2;
+
+ if (utf16len) *utf16len = (unsigned long) len;
+ memmove(*utf16, unicode, size);
+
+ return 0;
+}
+
+int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {
+#ifdef UNICODE
+ return to_utf16(utf8, buffer, buflen);
+#else
+ return to_utf8(utf8, buffer, buflen);
+#endif
+}
+
+int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {
+#ifdef UNICODE
+ return to_utf16(utf16, buffer, buflen);
+#else
+ return to_utf8(utf16, buffer, buflen);
+#endif
+}
--- /dev/null
+#ifndef UTF8_H
+#define UTF8_H
+
+void setup_utf8();
+void unsetup_utf8();
+int to_utf8(const wchar_t *, char **, unsigned long *);
+int to_utf8(const char *, char **, unsigned long *);
+int to_utf16(const char *, wchar_t **utf16, unsigned long *);
+int to_utf16(const wchar_t *, wchar_t **utf16, unsigned long *);
+int from_utf8(const char *, TCHAR **, unsigned long *);
+int from_utf16(const wchar_t *, TCHAR **, unsigned long *);
+
+#endif