X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=1f9e0ed1376cfb0cd22b0917a183de6e92c3d8d6;hb=62f28c55d444d846579159133dbe091809a2c08b;hp=4203c6156afc758f1746365a442b1c70b5cc081f;hpb=b99cfc38fcf4fbf5c87e23f93a02d1d6a6d931ca;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 4203c61..1f9e0ed 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -1,83 +1,312 @@ #include "nssm.h" extern unsigned long tls_index; +extern bool is_admin; +extern imports_t imports; -/* String function */ -int str_equiv(const char *a, const char *b) { - int i; +static TCHAR unquoted_imagepath[PATH_LENGTH]; +static TCHAR imagepath[PATH_LENGTH]; +static TCHAR imageargv0[PATH_LENGTH]; + +void nssm_exit(int status) { + free_imports(); + unsetup_utf8(); + exit(status); +} + +/* Are two strings case-insensitively equivalent? */ +int str_equiv(const TCHAR *a, const TCHAR *b) { + size_t len = _tcslen(a); + if (_tcslen(b) != len) return 0; + if (_tcsnicmp(a, b, len)) return 0; + return 1; +} + +/* Convert a string to a number. */ +int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) { + if (! string) return 1; + + *number = _tcstoul(string, bogus, 0); + if (**bogus) return 2; + + return 0; +} + +int str_number(const TCHAR *string, unsigned long *number) { + TCHAR *bogus; + return str_number(string, number, &bogus); +} + +/* Does a char need to be escaped? */ +static bool needs_escape(const TCHAR c) { + if (c == _T('"')) return true; + if (c == _T('&')) return true; + if (c == _T('%')) return true; + if (c == _T('^')) return true; + if (c == _T('<')) return true; + if (c == _T('>')) return true; + if (c == _T('|')) return true; + return false; +} + +/* Does a char need to be quoted? */ +static bool needs_quote(const TCHAR c) { + if (c == _T(' ')) return true; + if (c == _T('\t')) return true; + if (c == _T('\n')) return true; + if (c == _T('\v')) return true; + if (c == _T('"')) return true; + if (c == _T('*')) return true; + return needs_escape(c); +} + +/* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ */ +/* http://www.robvanderwoude.com/escapechars.php */ +int quote(const TCHAR *unquoted, TCHAR *buffer, size_t buflen) { + size_t i, j, n; + size_t len = _tcslen(unquoted); + if (len > buflen - 1) return 1; + + bool escape = false; + bool quotes = false; + + for (i = 0; i < len; i++) { + if (needs_escape(unquoted[i])) { + escape = quotes = true; + break; + } + if (needs_quote(unquoted[i])) quotes = true; + } + if (! quotes) { + memmove(buffer, unquoted, (len + 1) * sizeof(TCHAR)); + return 0; + } + + /* "" */ + size_t quoted_len = 2; + if (escape) quoted_len += 2; for (i = 0; ; i++) { - if (tolower(b[i]) != tolower(a[i])) return 0; - if (! a[i]) return 1; + n = 0; + + while (i != len && unquoted[i] == _T('\\')) { + i++; + n++; + } + + if (i == len) { + quoted_len += n * 2; + break; + } + else if (unquoted[i] == _T('"')) quoted_len += n * 2 + 2; + else quoted_len += n + 1; + if (needs_escape(unquoted[i])) quoted_len += n; } + if (quoted_len > buflen - 1) return 1; + + TCHAR *s = buffer; + if (escape) *s++ = _T('^'); + *s++ = _T('"'); + + for (i = 0; ; i++) { + n = 0; + + while (i != len && unquoted[i] == _T('\\')) { + i++; + n++; + } + + if (i == len) { + for (j = 0; j < n * 2; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + break; + } + else if (unquoted[i] == _T('"')) { + for (j = 0; j < n * 2 + 1; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + if (escape && needs_escape(unquoted[i])) *s++ = _T('^'); + *s++ = unquoted[i]; + } + else { + for (j = 0; j < n; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + if (escape && needs_escape(unquoted[i])) *s++ = _T('^'); + *s++ = unquoted[i]; + } + } + if (escape) *s++ = _T('^'); + *s++ = _T('"'); + *s++ = _T('\0'); + + return 0; +} + +/* Remove basename of a path. */ +void strip_basename(TCHAR *buffer) { + size_t len = _tcslen(buffer); + size_t i; + for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--); + /* X:\ is OK. */ + if (i && buffer[i - 1] == _T(':')) i++; + buffer[i] = _T('\0'); } /* How to use me correctly */ int usage(int ret) { - fprintf(stderr, "NSSM: The non-sucking service manager\n"); - fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE); - fprintf(stderr, "Usage: nssm