Command to dump service configuration.
[nssm.git] / nssm.cpp
index ff9eb52..9314753 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -6,6 +6,7 @@ extern imports_t imports;
 \r
 static TCHAR unquoted_imagepath[PATH_LENGTH];\r
 static TCHAR imagepath[PATH_LENGTH];\r
+static TCHAR imageargv0[PATH_LENGTH];\r
 \r
 /* Are two strings case-insensitively equivalent? */\r
 int str_equiv(const TCHAR *a, const TCHAR *b) {\r
@@ -30,6 +31,115 @@ int str_number(const TCHAR *string, unsigned long *number) {
   return str_number(string, number, &bogus);\r
 }\r
 \r
+/* Does a char need to be escaped? */\r
+static bool needs_escape(const TCHAR c) {\r
+  if (c == _T('"')) return true;\r
+  if (c == _T('&')) return true;\r
+  if (c == _T('%')) return true;\r
+  if (c == _T('^')) return true;\r
+  if (c == _T('<')) return true;\r
+  if (c == _T('>')) return true;\r
+  if (c == _T('|')) return true;\r
+  return false;\r
+}\r
+\r
+/* Does a char need to be quoted? */\r
+static bool needs_quote(const TCHAR c) {\r
+  if (c == _T(' ')) return true;\r
+  if (c == _T('\t')) return true;\r
+  if (c == _T('\n')) return true;\r
+  if (c == _T('\v')) return true;\r
+  if (c == _T('"')) return true;\r
+  if (c == _T('*')) return true;\r
+  return needs_escape(c);\r
+}\r
+\r
+/* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ */\r
+/* http://www.robvanderwoude.com/escapechars.php */\r
+int quote(const TCHAR *unquoted, TCHAR *buffer, size_t buflen) {\r
+  size_t i, j, n;\r
+  size_t len = _tcslen(unquoted);\r
+  if (len > buflen - 1) return 1;\r
+\r
+  bool escape = false;\r
+  bool quotes = false;\r
+\r
+  for (i = 0; i < len; i++) {\r
+    if (needs_escape(unquoted[i])) {\r
+      escape = quotes = true;\r
+      break;\r
+    }\r
+    if (needs_quote(unquoted[i])) quotes = true;\r
+  }\r
+  if (! quotes) {\r
+    memmove(buffer, unquoted, (len + 1) * sizeof(TCHAR));\r
+    return 0;\r
+  }\r
+\r
+  /* "" */\r
+  size_t quoted_len = 2;\r
+  if (escape) quoted_len += 2;\r
+  for (i = 0; ; i++) {\r
+    n = 0;\r
+\r
+    while (i != len && unquoted[i] == _T('\\')) {\r
+      i++;\r
+      n++;\r
+    }\r
+\r
+    if (i == len) {\r
+      quoted_len += n * 2;\r
+      break;\r
+    }\r
+    else if (unquoted[i] == _T('"')) quoted_len += n * 2 + 2;\r
+    else quoted_len += n + 1;\r
+    if (needs_escape(unquoted[i])) quoted_len += n;\r
+  }\r
+  if (quoted_len > buflen - 1) return 1;\r
+\r
+  TCHAR *s = buffer;\r
+  if (escape) *s++ = _T('^');\r
+  *s++ = _T('"');\r
+\r
+  for (i = 0; ; i++) {\r
+    n = 0;\r
+\r
+    while (i != len && unquoted[i] == _T('\\')) {\r
+      i++;\r
+      n++;\r
+    }\r
+\r
+    if (i == len) {\r
+      for (j = 0; j < n * 2; j++) {\r
+        if (escape) *s++ = _T('^');\r
+        *s++ = _T('\\');\r
+      }\r
+      break;\r
+    }\r
+    else if (unquoted[i] == _T('"')) {\r
+      for (j = 0; j < n * 2 + 1; j++) {\r
+        if (escape) *s++ = _T('^');\r
+        *s++ = _T('\\');\r
+      }\r
+      if (escape && needs_escape(unquoted[i])) *s++ = _T('^');\r
+      *s++ = unquoted[i];\r
+    }\r
+    else {\r
+      for (j = 0; j < n; j++) {\r
+        if (escape) *s++ = _T('^');\r
+        *s++ = _T('\\');\r
+      }\r
+      if (escape && needs_escape(unquoted[i])) *s++ = _T('^');\r
+      *s++ = unquoted[i];\r
+    }\r
+  }\r
+  if (escape) *s++ = _T('^');\r
+  *s++ = _T('"');\r
+  *s++ = _T('\0');\r
+\r
+  return 0;\r
+}\r
+\r
 /* Remove basename of a path. */\r
 void strip_basename(TCHAR *buffer) {\r
   size_t len = _tcslen(buffer);\r
@@ -104,6 +214,10 @@ const TCHAR *nssm_imagepath() {
   return imagepath;\r
 }\r
 \r
+const TCHAR *nssm_exe() {\r
+  return imageargv0;\r
+}\r
+\r
 int _tmain(int argc, TCHAR **argv) {\r
   check_console();\r
 \r
@@ -123,6 +237,8 @@ int _tmain(int argc, TCHAR **argv) {
   if (get_imports()) exit(111);\r
 \r
   /* Remember our path for later. */\r
+  _sntprintf_s(imageargv0, _countof(imageargv0), _TRUNCATE, _T("%s"), argv[0]);\r
+  PathQuoteSpaces(imageargv0);\r
   GetModuleFileName(0, unquoted_imagepath, _countof(unquoted_imagepath));\r
   GetModuleFileName(0, imagepath, _countof(imagepath));\r
   PathQuoteSpaces(imagepath);\r
@@ -146,9 +262,10 @@ int _tmain(int argc, TCHAR **argv) {
     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("install"))) {\r
       if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL));\r
+      create_messages();\r
       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"))) {\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
       /* There might be a password here. */\r