-
Notifications
You must be signed in to change notification settings - Fork 21
/
launcher.c
284 lines (247 loc) · 6.74 KB
/
launcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#define __USE_MINGW_ANSI_STDIO 1
#define _UNICODE
#define _WIN32_WINNT 0x0601
#define WIN32_LEAN_AND_MEAN
#define PSAPI_VERSION 1
#include <windows.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <wchar.h>
int _CRT_glob = 0;
#include "macros.h"
// if any of the properties change, it's best to use a brand new AppID
#define APPID_REVISION 9
static void ShowError(const wchar_t* desc, const wchar_t* err, const long code) {
wchar_t msg[1024];
swprintf(msg, 1024, L"%ls. Reason: %ls (0x%lx)", desc, err, code);
MessageBox(NULL, msg, L"Launcher error", MB_ICONEXCLAMATION | MB_OK);
}
static void ShowLastError(const wchar_t* desc) {
DWORD code;
wchar_t* err;
code = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&err, 0, NULL);
ShowError(desc, err, code);
LocalFree(err);
}
static void ShowErrno(const wchar_t* desc) {
wchar_t* err;
err = _wcserror(errno);
ShowError(desc, err, errno);
}
// adapted from http://www.partow.net/programming/hashfunctions/index.html
// MIT licensed
static unsigned int APHash(const wchar_t* str, size_t length) {
unsigned int hash = 0xAAAAAAAA;
size_t i = 0;
for (i = 0; i < length; ++str, ++i) {
hash ^= ((i & 1) == 0)
? ((hash << 7) ^ (*str) * (hash >> 3))
: (~((hash << 11) + ((*str) ^ (hash >> 5))))
;
}
return hash;
}
static PROCESS_INFORMATION StartChild(wchar_t* cmdline) {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
DWORD code;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
SetLastError(0);
code = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (code == 0) {
ShowLastError(L"Could not start the shell");
ShowError(L"The command was", cmdline, 0);
return pi;
}
return pi;
}
static wchar_t* SetEnv(wchar_t* conffile) {
int code;
size_t buflen;
size_t expandedlen;
wchar_t* tmp;
wchar_t* buf;
wchar_t* expanded;
wchar_t* msystem;
FILE* handle;
msystem = NULL;
handle = _wfopen(conffile, L"rt");
if (handle == NULL) {
ShowErrno(L"Could not open configuration file");
return msystem;
}
buflen = 512;
buf = (wchar_t*)malloc(buflen * sizeof(wchar_t));
*buf = L'\0';
expandedlen = 2 * buflen;
expanded = (wchar_t*)malloc(expandedlen * sizeof(wchar_t));
while (true) {
tmp = fgetws(buf + wcslen(buf), buflen - wcslen(buf), handle);
if (tmp == NULL && !feof(handle)) {
ShowErrno(L"Could not read from configuration file");
return NULL;
}
tmp = buf + wcslen(buf) - 1;
if (!feof(handle) && *tmp != L'\n') {
buflen *= 2;
buf = (wchar_t*)realloc(buf, buflen * sizeof(wchar_t));
continue;
}
if (!feof(handle)) {
*tmp = L'\0';
}
if (*buf != L'\0' && *buf != L'#') {
tmp = wcschr(buf, L'=');
if (tmp != NULL) {
*tmp++ = L'\0';
while (expandedlen < 32768) {
code = ExpandEnvironmentStrings(tmp, expanded, expandedlen);
if ((size_t)code <= expandedlen) {
break;
}
expandedlen *= 2;
expanded = (wchar_t*)realloc(expanded, expandedlen * sizeof(wchar_t));
}
if ((*tmp != L'\0' && code == 0) || (size_t)code > expandedlen) {
ShowLastError(L"Could not expand string");
}
if (0 == wcscmp(L"MSYSTEM", buf)) {
msystem = _wcsdup(expanded);
if (msystem == NULL) {
ShowError(L"Could not duplicate string", expanded, 0);
return NULL;
}
}
code = SetEnvironmentVariable(buf, expanded);
if (code == 0) {
ShowLastError(L"Could not set environment variable");
}
} else {
ShowError(L"Could not parse environment line", buf, 0);
}
}
*buf = L'\0';
if (feof(handle)) {
break;
}
}
code = fclose(handle);
if (code != 0) {
ShowErrno(L"Could not close configuration file");
}
return msystem;
}
int wmain(int argc, wchar_t* argv[]) {
PROCESS_INFORMATION child;
int code;
size_t buflen;
wchar_t* buf;
wchar_t* tmp;
wchar_t* args;
wchar_t* msystem;
wchar_t* msysdirhash;
wchar_t msysdir[PATH_MAX];
wchar_t exepath[PATH_MAX];
wchar_t confpath[PATH_MAX];
code = GetModuleFileName(NULL, exepath, sizeof(exepath) / sizeof(*exepath));
if (code == 0) {
ShowLastError(L"Could not determine executable path");
return __LINE__;
}
tmp = exepath;
while ((tmp = wcschr(tmp, L'/')) != NULL) {
*tmp = L'\\';
}
wcscpy(msysdir, exepath);
tmp = wcsrchr(msysdir, L'\\');
if (tmp == NULL) {
ShowError(L"Could not find root directory", msysdir, 0);
return __LINE__;
}
*tmp = L'\0';
msysdirhash = (wchar_t*)alloca(10 * sizeof(wchar_t)); // dot + unsigned int as %x + null
if (msysdirhash == NULL) {
ShowError(L"Could not allocate memory", L"", 0);
return __LINE__;
}
if (wcsicmp(msysdir, L"C:\\msys64") == 0) {
code = swprintf(msysdirhash, 10, L""); // no change in AppID for default installations
} else {
code = swprintf(msysdirhash, 10, L".%8x", APHash(msysdir, wcslen(msysdir)));
}
if (code < 0) {
ShowErrno(L"Could not write to buffer");
return __LINE__;
}
wcscpy(confpath, exepath);
tmp = confpath + wcslen(confpath) - 4;
if (wcsicmp(L".exe", tmp) != 0) {
ShowError(L"Could not find configuration file", confpath, 0);
return __LINE__;
}
*tmp++ = L'.';
*tmp++ = L'i';
*tmp++ = L'n';
*tmp++ = L'i';
if (argc > 1) {
code = SetEnvironmentVariable(L"CHERE_INVOKING", L"1");
if (code == 0) {
ShowLastError(L"Could not set environment variable");
}
}
msystem = SetEnv(confpath);
if (msystem == NULL) {
ShowError(L"Did not find the MSYSTEM variable", confpath, 0);
return __LINE__;
}
code = SetEnvironmentVariable(L"MSYSCON", L"mintty.exe");
if (code == 0) {
ShowLastError(L"Could not set environment variable");
}
args = GetCommandLine();
tmp = argv[0];
while (*tmp != L'\0') {
if (*args == *tmp) {
args++;
tmp++;
} else if (*args == L'"') {
args++;
} else {
ShowError(L"Could not parse command name", argv[0], 0);
return __LINE__;
}
}
while (*args == L'"') {
args++;
}
while (*args == L' ') {
args++;
}
code = -1;
buf = NULL;
buflen = 1024;
while (code < 0 && buflen < 8192) {
buf = (wchar_t*)realloc(buf, buflen * sizeof(wchar_t));
if (buf == NULL) {
ShowError(L"Could not allocate memory", L"", 0);
return __LINE__;
}
code = swprintf(buf, buflen, L"%ls\\usr\\bin\\mintty.exe -i '%ls' -o 'AppLaunchCmd=%ls' -o 'AppID=MSYS2.Shell.%ls.%d%ls' -o 'AppName=MSYS2 %ls Shell' -t 'MSYS2 %ls Shell' --store-taskbar-properties -- %ls %ls", msysdir, exepath, exepath, msystem, APPID_REVISION, msysdirhash, msystem, msystem, argc == 1 ? L"-" : L"/usr/bin/sh -lc '\"$@\"' sh", args);
buflen *= 2;
}
if (code < 0) {
ShowErrno(L"Could not write to buffer");
return __LINE__;
}
child = StartChild(buf);
if (child.hProcess == NULL) {
return __LINE__;
}
free(buf);
buf = NULL;
return 0;
}