wrappers: Split out shared helpers for native wrapper executables to a header

This commit is contained in:
Martin Storsjö committed 2019-05-06 11:04:53 +03:00
1 parent 66055dfe63
commit 97ca0681bc
6 files changed
+168 -234

No files matched your search

+7 -118
View File
@@ -16,9 +16,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef UNICODE
#define _UNICODE
#endif
#include "native-wrapper.h"
#ifndef CLANG
#define CLANG "clang"
@@ -27,90 +25,6 @@
#define DEFAULT_TARGET "x86_64-w64-mingw32"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <tchar.h>
#include <windows.h>
#include <process.h>
#define EXECVP_CAST
#else
#include <unistd.h>
typedef char TCHAR;
#define _T(x) x
#define _tcsrchr strrchr
#define _tcschr strchr
#define _tcsdup strdup
#define _tcscpy strcpy
#define _tcslen strlen
#define _tcscmp strcmp
#define _tperror perror
#define _texecvp execvp
#define _tmain main
#define EXECVP_CAST (char **)
#endif
#ifdef _UNICODE
#define TS "%ls"
#else
#define TS "%s"
#endif
static TCHAR *escape(const TCHAR *str) {
#ifdef _WIN32
TCHAR *out = malloc((_tcslen(str) * 2 + 3) * sizeof(*out));
TCHAR *ptr = out;
int i;
*ptr++ = '"';
for (i = 0; str[i]; i++) {
if (str[i] == '"') {
int j = i - 1;
// Before all double quotes, backslashes need to be escaped, but
// not elsewhere.
while (j >= 0 && str[j--] == '\\')
*ptr++ = '\\';
// Escape the next double quote.
*ptr++ = '\\';
}
*ptr++ = str[i];
}
// Any final backslashes, before the quote around the whole argument,
// need to be doubled.
int j = i - 1;
while (j >= 0 && str[j--] == '\\')
*ptr++ = '\\';
*ptr++ = '"';
*ptr++ = '\0';
return out;
#else
return _tcsdup(str);
#endif
}
static TCHAR *concat(const TCHAR *prefix, const TCHAR *suffix) {
int prefixlen = _tcslen(prefix);
int suffixlen = _tcslen(suffix);
TCHAR *buf = malloc((prefixlen + suffixlen + 1) * sizeof(*buf));
_tcscpy(buf, prefix);
_tcscpy(buf + prefixlen, suffix);
return buf;
}
static TCHAR *_tcsrchrs(const TCHAR *str, TCHAR char1, TCHAR char2) {
TCHAR *ptr1 = _tcsrchr(str, char1);
TCHAR *ptr2 = _tcsrchr(str, char2);
if (!ptr1)
return ptr2;
if (!ptr2)
return ptr1;
if (ptr1 < ptr2)
return ptr2;
return ptr1;
}
#ifdef _WIN32
static int filter_line = 0, last_char = '\n';
static void filter_stderr(char *buf, int n) {
@@ -222,39 +136,14 @@ static int exec_filtered(const TCHAR **argv) {
#endif
int _tmain(int argc, TCHAR* argv[]) {
const TCHAR *argv0 = argv[0];
const TCHAR *sep = _tcsrchrs(argv0, '/', '\\');
TCHAR *dir = _tcsdup(_T(""));
const TCHAR *basename = argv0;
if (sep) {
dir = _tcsdup(argv0);
dir[sep + 1 - argv0] = '\0';
basename = sep + 1;
}
#ifdef _WIN32
TCHAR module_path[8192];
GetModuleFileName(NULL, module_path, sizeof(module_path)/sizeof(module_path[0]));
TCHAR *sep2 = _tcsrchr(module_path, '\\');
if (sep2) {
sep2[1] = '\0';
dir = _tcsdup(module_path);
}
#endif
basename = _tcsdup(basename);
TCHAR *period = _tcschr(basename, '.');
if (period)
*period = '\0';
TCHAR *dash = _tcsrchr(basename, '-');
const TCHAR *target = basename;
const TCHAR *exe = basename;
if (dash) {
*dash = '\0';
exe = dash + 1;
} else {
const TCHAR *dir;
const TCHAR *target;
const TCHAR *exe;
split_argv(argv[0], &dir, NULL, &target, &exe);
if (!target)
target = _T(DEFAULT_TARGET);
}
TCHAR *arch = _tcsdup(target);
dash = _tcschr(arch, '-');
TCHAR *dash = _tcschr(arch, '-');
if (dash)
*dash = '\0';
TCHAR *target_os = _tcsrchr(target, '-');