X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=b22cb920124f3173cc9f4b2c21c3ec0f805a2386;hb=2a259b54caa08ef6d4dbc99f6fe7ec97837c25e8;hp=7a7311425f7fff20207c8389dec37b5ca03007b9;hpb=fb6318bf814fd7f3445de8c83b7620e1b6b3c197;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 7a73114..b22cb92 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -2,29 +2,158 @@ extern unsigned long tls_index; extern bool is_admin; +extern imports_t imports; + +static TCHAR unquoted_imagepath[PATH_LENGTH]; +static TCHAR imagepath[PATH_LENGTH]; +static TCHAR imageargv0[PATH_LENGTH]; + +/* 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++) { + 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('"'); -/* String function */ -int str_equiv(const char *a, const char *b) { - int i; 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) { + 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